Maximum Vacation Days

Updated on 13 June, 2025
Maximum Vacation Days header image

Problem Statement

In this scenario, an employee is offered an opportunity to travel among n distinct cities, identified by indexes from 0 to n-1, and gather algorithm problems. Alongside this, the employee is invited to take vacations in selected cities during certain weeks. The chief objective is to plan this travel in a way that maximizes vacation time while adhering to specified rules involving travel and stay.

We start in city 0 on a Monday. Travelling is depicted through an n x n matrix flights, where each entry flights[i][j] is a binary value indicating direct flight availability on Mondays from city i to city j. 0 implies no flight, and 1 means a flight is available.

Each city offers a limited number of vacation days across k weeks, presented in an n x k matrix days, with days[i][j] outlining maximum allowable vacation days in city i during week j. The task is to leverage these matrices to calculate and return the maximum vacation days possible over k weeks under the constraints provided.

Examples

Example 1

Input:

flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[1,3,1],[6,0,3],[3,3,3]]

Output:

12

Explanation:

One of the best strategies is:
1st week : fly from city 0 to city 1 on Monday, and play 6 days and work 1 day.
(Although you start at city 0, you could also fly to and start at other cities since it is Monday.)
2nd week : fly from city 1 to city 2 on Monday, and play 3 days and work 4 days.
3rd week : stay at city 2, and play 3 days and work 4 days.
Ans = 6 + 3 + 3 = 12.

Example 2

Input:

flights = [[0,0,0],[0,0,0],[0,0,0]], days = [[1,1,1],[7,7,7],[7,7,7]]

Output:

3

Explanation:

Since there are no flights that enable you to move to another city, you have to stay at city 0 for the whole 3 weeks.
For each week, you only have one day to play and six days to work.
So the maximum number of vacation days is 3.
Ans = 1 + 1 + 1 = 3.

Example 3

Input:

flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[7,0,0],[0,7,0],[0,0,7]]

Output:

21

Explanation:

One of the best strategies is:
1st week : stay at city 0, and play 7 days.
2nd week : fly from city 0 to city 1 on Monday, and play 7 days.
3rd week : fly from city 1 to city 2 on Monday, and play 7 days.
Ans = 7 + 7 + 7 = 21.

Constraints

  • n == flights.length
  • n == flights[i].length
  • n == days.length
  • k == days[i].length
  • 1 <= n, k <= 100
  • flights[i][j] is either 0 or 1.
  • 0 <= days[i][j] <= 7

Approach and Intuition

To tackle this problem, we can utilize a dynamic programming strategy which will efficiently manage the complexity induced by multiple cities and weeks:

  1. DP Table Initialization: Create a table dp where dp[w][c] represents the maximum vacation days obtainable from week w in city c. Initialize all values to a minimal default (e.g., -1) since we need to compute them.

  2. Base Case Setup: At week 0, you are at city 0. Hence, dp[0][0] = days[0][0] assuming you are taking a vacation in city 0 in the first week.

  3. Iterative Week-wise Calculation: For each week from 0 to k-1, and for each city c, do:

    • If the employee can stay in city c from the previous week or arrive from any city i with a valid flight connection (flights[i][c] == 1), compare:

      • The vacation days from staying in city c from the previous week plus the days for the current week from matrix days.
      • Vacation days achievable by flying in from each possible city i.
    • The above comparison will set dp[w][c] for each city c and week w.

  4. End Week Calculation: At the conclusion of k weeks, look through dp[k-1] to find the highest value, which represents the maximal vacation days the employee can achieve.

  5. Considerations Based on Restrictions: Account for cases where the employee cannot travel to any city from their current location. In such instances, they will have to remain in their current city and work except for the allowed vacation days.

By systematically updating the dp table and ensuring that each week accounts for both previous states and travel possibilities, the solution efficiently computes the maximum vacation days using the optimal substructure property of dynamic programming. The complexity here is mainly influenced by the number of cities (n) and weeks (k), leading to an overall time complexity of approximately O(n² * k), which is feasible under given constraints.

Solutions

  • Java
java
public class Solution {
    public int maximizeHolidays(int[][] flightMap, int[][] stayDays) {
        if (stayDays.length == 0 || flightMap.length == 0) return 0;
        int[] maxDaysDP = new int[stayDays.length];
        for (int week = stayDays[0].length - 1; week >= 0; week--) {
            int[] nextWeekMax = new int[stayDays.length];
            for (int city = 0; city < stayDays.length; city++) {
                nextWeekMax[city] = stayDays[city][week] + maxDaysDP[city];
                for (int nextCity = 0; nextCity < stayDays.length; nextCity++) {
                    if (flightMap[city][nextCity] == 1) {
                        nextWeekMax[city] = Math.max(stayDays[nextCity][week] + maxDaysDP[nextCity], nextWeekMax[city]);
                    }
                }
            }
            maxDaysDP = nextWeekMax;
        }

        return maxDaysDP[0];
    }
}

In the "Maximum Vacation Days" Java program, there is a task to calculate the maximum number of vacation days one can spend given conditions on inter-city travel. The method maximizeHolidays is designed to solve this problem using dynamic programming principles. Here's a breakdown of how the solution works:

  • Initialize a check to ensure there are cities and weeks of data available. If not, return 0 as no vacation days can be planned.

  • Create an array maxDaysDP to keep track of the maximum stay days that can be achieved from each city at the beginning of each week.

  • Iterate from the last week to the first week. For each city during a week, calculate the maximum vacation days if starting from that city.

  • Update nextWeekMax for each city by adding the stay days of that city for the current week to the maximum of the days calculated for all achievable cities in the following weeks.

  • For each possible destination city from the current city, if the travel is allowed, calculate the maximum of staying in the current city or traveling to another city.

  • Assign the values from nextWeekMax to maxDaysDP to carry forward the maximum calculations to the next iteration (i.e., previous week).

  • The maximum value for starting the trip from the first city and planning optimally is found in maxDaysDP[0] after all iterations.

This approach ensures each decision is based on the optimal choices made in subsequent weeks, and it efficiently finds the maximum vacation days possible.

Comments

No comments yet.