Problem Statement
You are given two integer arrays:
h[i]→ number of tasks completed by choosing a high-effort task on dayi.l[i]→ number of tasks completed by choosing a low-effort task on dayi.
For every day, we can choose one of three options:
Do no task.
Perform a low-effort task.
Perform a high-effort task.
The important condition is:
A high-effort task can be performed only on the first day or when no task was performed on the previous day.
We need to find the maximum total number of tasks that can be completed.
Example
Consider:
h = [2, 8, 1]
l = [1, 2, 1]
One possible selection is:
Day 0 → High effort = 2
Day 1 → No task
Day 2 → High effort = 1
Total:
2 + 1 = 3
But a better choice is:
Day 0 → Low effort = 1
Day 1 → High effort = 8
Total:
1 + 8 = 9
Therefore:
h = [2, 8, 1]
l = [1, 2, 1]
Output = 9
The important constraint is that high effort requires the previous day to have no task.
Dynamic Programming Idea
The brute-force approach would try every possible choice for every day.
For each day:
No task
Low effort
High effort
This gives roughly 3^n possibilities, which is far too expensive for:
n <= 100000
Instead, we use Dynamic Programming (DP).
The main observation is to consider the choices available on the current day.
If We Perform a Low-Effort Task Today
There is no restriction on what we did yesterday.
Therefore:
Today's total = best answer until yesterday + l[i]
If We Perform a High-Effort Task Today
We cannot perform any task yesterday.
Therefore:
Today's total = best answer until day i-2 + h[i]
So the recurrence becomes:
dp[i] = max(
dp[i-1] + l[i],
dp[i-2] + h[i]
)
Why dp[i-2] for High Effort?
This is the most important part of the problem.
Suppose we are on day i and want to perform a high-effort task.
The previous day i-1 must have no task.
Therefore, we cannot simply use:
dp[i-1] + h[i]
because dp[i-1] might represent a solution where a task was performed on day i-1.
Instead, we go back to:
dp[i-2]
Then we skip day i-1 and perform the high-effort task on day i.
So:
high = dp[i-2] + h[i]
DP Recurrence
For every day i:
Option 1: Low-Effort Task
low = dp[i-1] + l[i]
Option 2: High-Effort Task
high = dp[i-2] + h[i]
Take the maximum:
dp[i] = max(low, high)
The do nothing option is implicitly covered because the DP value represents the best result achievable up to that point, and the values are non-negative.
Optimizing Space
Normally, we might create an array:
int[] dp = new int[n];
But we don't actually need the entire DP array.
At day i, we only need:
dp[i-1]
dp[i-2]
Therefore, we can maintain only two variables:
prev1 = dp[i-1]
prev2 = dp[i-2]
This reduces the auxiliary space from:
O(n)
to:
O(1)
Java Solution
class Solution {
public int maxTask(int[] h, int[] l) {
int n = h.length;
int prev2 = 0; // dp[i-2]
int prev1 = 0; // dp[i-1]
for (int i = 0; i < n; i++) {
int high = h[i] + prev2;
int low = l[i] + prev1;
int current = Math.max(low, high);
prev2 = prev1;
prev1 = current;
}
return prev1;
}
}

Join the conversation! Your thoughts help the community grow.