BudiBadu Logo

Lantern Crew Rest Planner

Dynamic Programming Hard 2 views
Like12

The lantern orchestra sets up over several nights, and each evening offers a choice between rehearsing quietly, polishing the stage, or performing a glowing showcase. Every option awards a different number of harmony tokens that pay the crew, yet the conductor enforces rest patterns so the team does not burn out. After any showcase night, the crew must observe a one-night cooldown before another stage polishing session, and two consecutive polishing nights are also forbidden because the same crew handles both shifts. The conductor wants the schedule that yields the highest total tokens under these fatigue constraints.

You receive two integer arrays of equal length: prep lists the tokens earned by dedicating day i to stage polishing, and show lists the tokens earned by performing the full showcase on day i. Each day you must select exactly one of three statuses—rest (worth 0), polish, or showcase—while obeying the rules: polish cannot follow polish, and showcase cannot follow showcase. A polish day is also disallowed immediately after a showcase because that crew still handles cooldown teardown. Rest has no restrictions and may appear anytime. If no days exist, the optimal reward is 0. Build the answer with dynamic programming without mutating the input arrays, and return the maximum achievable token total so the conductor can book musicians, schedule lighting technicians, and promise guests a sustainable run of lantern magic.

Example 1:

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.

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 = [], show = []
Output: 0
Explanation: With no scheduled nights, the reward is zero.

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;
    }
}