Energy-Constrained Interval Task Scheduling
An autonomous rover needs to select a set of non-overlapping tasks to execute in order to maximize its total profit.
You are given:
tasks: A list of objects, where each task $i$ has:start: The start time $S_i$ of the task ($S_i \ge 0$).end: The finish time $F_i$ of the task ($F_i > S_i$).profit: The profit $P_i$ earned upon completing the task ($P_i > 0$).energy_cost: The energy $C_i$ consumed by executing the task ($C_i \ge 0$).
E_max: An integer representing the rover's maximum energy capacity.R: An integer representing the energy recharge rate per unit of time during idle periods.
Rules:
- Execution: The rover can execute at most one task at any time. If task $i$ is chosen, it runs continuously from $S_i$ to $F_i$. Two tasks $i$ and $j$ do not overlap if $F_i \le S_j$ or $F_j \le S_i$.
- Energy Capacity & Start State: The rover starts at time $t = 0$ with full energy $E_{max}$. Its energy level can never exceed $E_{max}$ or drop below $0$.
- Task Energy Consumption: To execute task $i$, the rover must have at least $C_i$ energy at time $S_i$. Immediately after the task completes at time $F_i$, its energy decreases by $C_i$.
- Idle Energy Recharge: During any idle period from $t_1$ to $t_2$ (where no task is running), the rover recharges energy at rate $R$. Energy at $t_2$ becomes $\min(E_{max}, e_{t_1} + R \times (t_2 - t_1))$.
Return the maximum total profit obtainable by selecting a valid sequence of tasks.
[ "[{\"start\":1,\"end\":3,\"profit\":10,\"energy_cost\":5},{\"start\":4,\"end\":6,\"profit\":20,\"energy_cost\":8},{\"start\":7,\"end\":9,\"profit\":15,\"energy_cost\":3}]", "10", "1" ]
Explanation. Task 0 (1..3) leaves 5 energy at t=3. By t=4, energy is 6, which is insufficient for Task 1 (cost 8). Instead, selecting Task 1 (profit 20) as the first task leaves 2 energy at t=6. By t=7, energy recharges to 3, allowing Task 2 (profit 15) to run. Total profit = 20 + 15 = 35.
[ "[{\"start\":0,\"end\":5,\"profit\":100,\"energy_cost\":50}]", "20", "2" ]
Explanation. The required energy cost of 50 exceeds the maximum energy capacity of 20, so no task can be executed.
[ "[{\"start\":1,\"end\":3,\"profit\":30,\"energy_cost\":10},{\"start\":5,\"end\":7,\"profit\":40,\"energy_cost\":10},{\"start\":9,\"end\":11,\"profit\":50,\"energy_cost\":10}]", "10", "5" ]
Explanation. With a high recharge rate (R=5), 2 time units of idle time between tasks fully recharges energy back to E_max = 10. All three tasks can be executed sequentially for a total profit of 30 + 40 + 50 = 120.
[ "[{\"start\":0,\"end\":4,\"profit\":50,\"energy_cost\":20},{\"start\":0,\"end\":2,\"profit\":20,\"energy_cost\":5},{\"start\":3,\"end\":5,\"profit\":60,\"energy_cost\":15}]", "20", "2" ]
Explanation. Choosing Task 0 (profit 50) prevents executing Task 2 because Task 0 ends at t=4 while Task 2 starts at t=3. Choosing Task 1 (profit 20) finishes at t=2 with 15 energy remaining. By t=3, energy recharges to 17, allowing Task 2 (profit 60) to execute. Total profit = 20 + 60 = 80.
[ "[]", "50", "1" ]
Explanation. When there are no candidate tasks, maximum achievable profit is 0.
Follow-up: Can you optimize the solution to run in O(N * E_max) time using a cumulative maximum transition array per energy state?
0 <= tasks.length <= 300 0 <= start < end <= 10^5 1 <= profit <= 10^4 0 <= energy_cost <= 500 1 <= E_max <= 500 0 <= R <= 100 All start times, end times, profits, energy costs, E_max, and R are integers.
- Views
- 2