Code Logo

Max Vacation Profit

Published at05 Jan 2026
Hard 16 views
Like14

You are given a list of enjoyment values for vacation days, and the goal is to pick days that maximize the total enjoyment. The catch is that you cannot take two adjacent days.

This is the same pattern as choosing from a row of houses or jobs where taking one option blocks the next one. A large value today might look tempting, but sometimes skipping it leads to a better total later.

For example, if enjoyment = [3,2,5,10,7], the answer is 15. One best plan is to take days with enjoyment 3, 5, and 7. If the list is empty, the answer is 0.

So the task is to find the maximum sum you can build without ever choosing two neighboring days.

Example Input & Output

Example 1
Input
enjoyment = []
Output
0
Explanation

No days, no enjoyment.

Example 2
Input
enjoyment = [2,1,4,5,3,1,1,3]
Output
10
Explanation

Optimal plan is days 0,2,4,7: 2+4+3+3 = 12, but cooldown after day 4 prevents day 5, so best consistent plan yields 10.

Example 3
Input
enjoyment = [3,2,5,10,7]
Output
15
Explanation

Visit on days 0,2,4 (3+5+7) or days 1,3 (2+10); the maximum is 15.

Algorithm Flow

Recommendation Algorithm Flow for Max Vacation Profit
Recommendation Algorithm Flow for Max Vacation Profit

Solution Approach

This is the classic house robber DP. At each day, you have two choices: skip it, or take it. If you take it, then the previous day cannot also be taken.

Let dp[i] mean the best total we can achieve using the first i + 1 days. Then the recurrence is:

dp[i] = Math.max(dp[i - 1], enjoyment[i] + dp[i - 2])

The first option means we skip day i. The second option means we take day i, so we add its value to the best answer from two days earlier.

The base cases are simple:

dp[0] = enjoyment[0]

dp[1] = max(enjoyment[0], enjoyment[1])

Then we build forward. The last DP value is the answer. This runs in O(n) time, and it can be optimized to O(1) extra space with two running variables.

Best Answers

java
import java.util.*;
class Solution {
    public int calculate_max_profit(int[][] jobs) {
        Arrays.sort(jobs, (a, b) -> a[1] - b[1]);
        int n = jobs.length;
        int[] dp = new int[n + 1];
        int[] ends = new int[n];
        for (int i = 0; i < n; i++) ends[i] = jobs[i][1];
        for (int i = 1; i <= n; i++) {
            int start = jobs[i-1][0];
            int profit = jobs[i-1][2];
            int idx = Arrays.binarySearch(ends, 0, i - 1, start);
            if (idx < 0) idx = -(idx + 1);
            else idx = idx + 1;
            dp[i] = Math.max(dp[i-1], dp[idx] + profit);
        }
        return dp[n];
    }
}