KatInterview

568. Maximum Vacation Days

HardSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the maximum number of vacation days one can accumulate over k weeks by traveling among n cities. We start at city 0 in week 0. Each week, on Monday, we can either stay in the current city or take a flight to a connected city. The vacation days for a week depend on the city we are in for that week.

Solution

Maximum Vacation Days in Python

class Solution:
    def maxVacationDays(self, flights: List[List[int]], days: List[List[int]]) -> int:
        n = len(flights)
        k = len(days[0])

        # dp[j] will store the maximum vacation days at the end of a week, ending in city j.
        dp = [-1] * n

        # --- Week 0 ---
        # Initially at city 0. Can stay or fly to a connected city.
        dp[0] = days[0][0]
        for j in range(1, n):
            if flights[0][j] == 1:
                dp[j] = days[j][0]

        # --- Week 1 to k-1 ---
        for i in range(1, k):
            temp_dp = [-1] * n
            for j in range(n):  # destination city j for week i
                for p in range(n):  # source city p from week i-1
                    # If it was possible to be in city p at the end of last week
                    if dp[p] != -1:
                        # and we can travel from p to j (fly or stay)
                        if p == j or flights[p][j] == 1:
                            current_vacation = dp[p] + days[j][i]
                            if current_vacation > temp_dp[j]:
                                temp_dp[j] = current_vacation
            dp = temp_dp

        # Find the maximum vacation days across all cities after k weeks
        max_vacation = 0
        for val in dp:
            if val > max_vacation:
                max_vacation = val
        
        return max_vacation

Maximum Vacation Days in Java

class Solution {
    public int maxVacationDays(int[][] flights, int[][] days) {
        int n = flights.length;
        int k = days[0].length;

        // dp[j] will store the maximum vacation days at the end of a week, ending in city j.
        int[] dp = new int[n];
        Arrays.fill(dp, -1);

        // --- Week 0 ---
        // Initially at city 0. Can stay or fly to a connected city.
        dp[0] = days[0][0];
        for (int j = 1; j < n; j++) {
            if (flights[0][j] == 1) {
                dp[j] = days[j][0];
            }
        }

        // --- Week 1 to k-1 ---
        for (int i = 1; i < k; i++) {
            int[] tempDp = new int[n];
            Arrays.fill(tempDp, -1);
            for (int j = 0; j < n; j++) { // destination city j for week i
                for (int p = 0; p < n; p++) { // source city p from week i-1
                    // If it was possible to be in city p at the end of last week
                    if (dp[p] != -1) {
                        // and we can travel from p to j (fly or stay)
                        if (p == j || flights[p][j] == 1) {
                            int currentVacation = dp[p] + days[j][i];
                            if (currentVacation > tempDp[j]) {
                                tempDp[j] = currentVacation;
                            }
                        }
                    }
                }
            }
            dp = tempDp;
        }

        // Find the maximum vacation days across all cities after k weeks
        int maxVacation = 0;
        for (int val : dp) {
            if (val > maxVacation) {
                maxVacation = val;
            }
        }

        return maxVacation;
    }
}

Maximum Vacation Days in C++

class Solution {
public:
    int maxVacationDays(vector<vector<int>>& flights, vector<vector<int>>& days) {
        int n = flights.size();
        int k = days[0].size();

        // dp[j] will store the maximum vacation days at the end of a week, ending in city j.
        vector<int> dp(n, -1);

        // --- Week 0 ---
        // Initially at city 0. Can stay or fly to a connected city.
        dp[0] = days[0][0];
        for (int j = 1; j < n; ++j) {
            if (flights[0][j] == 1) {
                dp[j] = days[j][0];
            }
        }

        // --- Week 1 to k-1 ---
        for (int i = 1; i < k; ++i) {
            vector<int> temp_dp(n, -1);
            for (int j = 0; j < n; ++j) { // destination city j for week i
                for (int p = 0; p < n; ++p) { // source city p from week i-1
                    // If it was possible to be in city p at the end of last week
                    if (dp[p] != -1) {
                        // and we can travel from p to j (fly or stay)
                        if (p == j || flights[p][j] == 1) {
                            int current_vacation = dp[p] + days[j][i];
                            if (current_vacation > temp_dp[j]) {
                                temp_dp[j] = current_vacation;
                            }
                        }
                    }
                }
            }
            dp = temp_dp;
        }

        // Find the maximum vacation days across all cities after k weeks
        int max_vacation = 0;
        for (int val : dp) {
            if (val > max_vacation) {
                max_vacation = val;
            }
        }

        return max_vacation;
    }
};

Maximum Vacation Days in TypeScript

function maxVacationDays(flights: number[][], days: number[][]): number {
    const n = flights.length;
    const k = days[0].length;

    // dp[j] will store the maximum vacation days at the end of a week, ending in city j.
    let dp: number[] = new Array(n).fill(-1);

    // --- Week 0 ---
    // Initially at city 0. Can stay or fly to a connected city.
    dp[0] = days[0][0];
    for (let j = 1; j < n; j++) {
        if (flights[0][j] === 1) {
            dp[j] = days[j][0];
        }
    }

    // --- Week 1 to k-1 ---
    for (let i = 1; i < k; i++) {
        const tempDp: number[] = new Array(n).fill(-1);
        for (let j = 0; j < n; j++) { // destination city j for week i
            for (let p = 0; p < n; p++) { // source city p from week i-1
                // If it was possible to be in city p at the end of last week
                if (dp[p] !== -1) {
                    // and we can travel from p to j (fly or stay)
                    if (p === j || flights[p][j] === 1) {
                        const currentVacation = dp[p] + days[j][i];
                        if (currentVacation > tempDp[j]) {
                            tempDp[j] = currentVacation;
                        }
                    }
                }
            }
        }
        dp = tempDp;
    }

    // Find the maximum vacation days across all cities after k weeks
    let maxVacation = 0;
    for (const val of dp) {
        if (val > maxVacation) {
            maxVacation = val;
        }
    }

    return maxVacation;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.