BudiBadu Logo

Lantern Crew Rest Planner

Dynamic Programming Hard 2 views
Like12

The lantern crew sets up over several nights choosing between stage polishing or a glowing showcase Each awards harmony tokens but the conductor enforces strict rest rules polish cannot follow polish and showcase cannot follow showcase Also polish is disallowed immediately after a showcase due to cooldown tasks Your goal in Lantern Crew Rest Planner is to find the maximum possible tokens the team can earn The secret sauce here is Dynamic Programming DP You receive two equal-length arrays prep polishing tokens and show showcase tokens Each day you pick one of three statuses rest 0 polish or showcase By tracking the best possible total for each state on day i based on valid previous-day choices you find the optimal schedule without burning out the crew If no days exist return 0 This planning tool ensures the festival stays sustainable while maximizing the team''s reward It''s a classic resource optimization challenge with state-based constraints Because this is a counting optimization-style challenge dynamic programming is usually the safest approach define what each index or state means initialize valid base states and make transitions explicit If the problem uses modulo arithmetic apply modulo at every accumulation step so large intermediate totals never corrupt the final answer At hard difficulty hidden tests usually combine scale with edge behavior so the implementation must be both asymptotically efficient and logically strict Define transitions unambiguously prevent stale-state contamination and confirm tie-breaking or fallback rules are encoded

Examples

Example 1
Input
prep = [], show = []
Output
0
Explanation

With no scheduled nights, the reward is zero.

Example 2
Input
prep = [2,2,2], show = [3,3,3]
Output
8
Explanation

Perform day 1, rest day 2, polish day 3 yields total 8 tokens.

Example 3
Input
prep = [3,2,4,2], show = [5,3,6,4]
Output
15
Explanation

One optimal plan polishes day 1, rests day 2, performs day 3, and polishes day 4.

Algorithm Flow

Recommendation Algorithm Flow for Lantern Crew Rest Planner - Budibadu
Recommendation Algorithm Flow for Lantern Crew Rest Planner - Budibadu

Best Answers

java
import java.util.*;
class Solution {
    public int calculate_max_crews(int[][] intervals) {
        if (intervals.length == 0) return 0;
        Arrays.sort(intervals, (a, b) -> a[1] - b[1]);
        int count = 1;
        int lastEnd = intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] >= lastEnd) {
                count++;
                lastEnd = intervals[i][1];
            }
        }
        return count;
    }
}