Quick Overview

Choose which city to spend each of k weeks in, given a directed flight matrix and per-city weekly vacation limits, so the total number of vacation days is as large as possible. Tests dynamic programming over weeks and cities with a rolling state array.

Plan weekly flights between cities to maximize total vacation days

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are planning a trip of `k` weeks across `n` cities, numbered `0` to `n - 1`. You start in city `0` on the Monday morning of the first week. Choose where to spend each week so that the total number of vacation days you take is as large as possible. Flights are given as an `n x n` matrix `flights`. `flights[i][j] = 1` means there is a direct flight from city `i` to city `j`, and `flights[i][j] = 0` means there is none. `flights[i][i]` is always `0`, but staying in your current city is always allowed. You can take at most one flight per week, and only on Monday morning. Flight time is ignored, so the city you are in after Monday's flight (or after choosing to stay) is where you spend that whole week. Vacation limits are given as an `n x k` matrix `days`. `days[i][j]` is the maximum number of vacation days you can take in city `i` during week `j`. You may spend a week in a city even when its limit is below 7; the extra days are work days and do not count. If you fly to a city on a Monday, that week's vacation days count toward the destination city. Return the maximum total number of vacation days you can take over the `k` weeks. ### Function Signature ```python def max_vacation_days(flights: list[list[int]], days: list[list[int]]) -> int: ``` ### Rules - Flights are directed: `flights[i][j] = 1` says nothing about `flights[j][i]`. - On the first Monday you may already fly out of city `0` along any of its flights, or stay. - Week `w` uses column `w` of `days`, for `w` from `0` to `k - 1`. - Return a single integer, the best possible total. ### Constraints - `n == flights.length == flights[i].length == days.length` - `k == days[i].length` - `1 <= n <= 100` - `1 <= k <= 100` - `flights[i][j]` is either `0` or `1` - `0 <= days[i][j] <= 7` ### Examples ```text Input: flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[1,3,1],[6,0,3],[3,3,3]] Output: 12 ``` Every city has a flight to every other city, so any sequence of cities is possible and the best total is the largest value in each week's column: 6 + 3 + 3 = 12. One such schedule flies to city 1 for week 0, flies to city 2 for week 1, and stays in city 2 for week 2. ```text Input: flights = [[0,0,0],[0,0,0],[0,0,0]], days = [[1,1,1],[7,7,7],[7,7,7]] Output: 3 ``` There are no flights, so you spend all three weeks in city `0` and take 1 + 1 + 1 = 3 days. The 7s in cities 1 and 2 are out of reach. ```text Input: flights = [[0,1],[0,0]], days = [[2,7,1],[5,0,6]] Output: 15 ``` The only flight goes from city 0 to city 1, with no way back. Staying in city 0 for weeks 0 and 1 (2 + 7) and flying to city 1 on the third Monday (6) gives 15; the other three possible schedules give 10, 8 and 11.

Overview: Choose which city to spend each of k weeks in, given a directed flight matrix and per-city weekly vacation limits, so the total number of vacation days is as large as possible. Tests dynamic programming over weeks and cities with a rolling state array.

You are planning a trip of `k` weeks across `n` cities, numbered `0` to `n - 1`. You start in city `0` on the Monday morning of the first week. Choose where to spend each week so that the total number of vacation days you take is as large as possible. Flights are given as an `n x n` matrix `flights`. `flights[i][j] = 1` means there is a direct flight from city `i` to city `j`, and `flights[i][j] = 0` means there is none. Flights are directed: `flights[i][j] = 1` says nothing about `flights[j][i]`. `flights[i][i]` is always `0`, but staying in your current city is always allowed. You can take at most one flight per week, and only on Monday morning. Flight time is ignored, so the city you are in after Monday's flight (or after choosing to stay) is where you spend that whole week. On the first Monday you may already fly out of city `0` along any of its flights, or stay. Vacation limits are given as an `n x k` matrix `days`. `days[i][j]` is the maximum number of vacation days you can take in city `i` during week `j`, and week `w` uses column `w` of `days` for `w` from `0` to `k - 1`. You may spend a week in a city even when its limit is below 7; the extra days are work days and do not count. If you fly to a city on a Monday, that week's vacation days count toward the destination city. Implement `max_vacation_days(flights, days)` so that it returns a single integer: the maximum total number of vacation days you can take over the `k` weeks. Several schedules may reach that total; only the total is returned. The result is at most `7 * 100 = 700`, so it fits in a 32-bit signed integer. ### Example 1 ```text Input: flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[1,3,1],[6,0,3],[3,3,3]] Output: 12 ``` Every city has a flight to every other city, so any sequence of cities is possible and the best total is the largest value in each week's column: 6 + 3 + 3 = 12. One such schedule flies to city 1 for week 0, flies to city 2 for week 1, and stays in city 2 for week 2. ### Example 2 ```text Input: flights = [[0,1],[0,0]], days = [[2,7,1],[5,0,6]] Output: 15 ``` The only flight goes from city 0 to city 1, with no way back. Staying in city 0 for weeks 0 and 1 (2 + 7) and flying to city 1 on the third Monday (6) gives 15; the other three possible schedules give 10, 8 and 11. ### Constraints - `n == flights.length == flights[i].length == days.length` - `k == days[i].length` - `1 <= n <= 100` - `1 <= k <= 100` - `flights[i][j]` is either `0` or `1`, and `flights[i][i] == 0` - `0 <= days[i][j] <= 7`

Constraints

  • n == flights.length == flights[i].length == days.length
  • k == days[i].length
  • 1 <= n <= 100
  • 1 <= k <= 100
  • flights[i][j] is either 0 or 1
  • flights[i][i] == 0, and staying in the current city is always allowed
  • 0 <= days[i][j] <= 7
  • The answer is at most 700, so it fits in a 32-bit signed integer

Examples

Input: ([[0]], [[7]])

Expected Output: 7

Explanation: Minimum shape n = 1, k = 1 with the per-cell maximum 7; the only schedule stays in city 0.

Input: ([[0]], [[3, 0, 7, 2, 5]])

Expected Output: 17

Explanation: One city for 5 weeks (row length 5): staying is the only option, so the answer is 3 + 0 + 7 + 2 + 5 = 17.

Hints

  1. In any week you are either in the city you were in the week before or one direct flight away from it, and before week 0 you are in city 0.
  2. A city with large values may be unreachable, or reachable only after several weeks of single flights.
  3. Picking the best reachable city each week can fall short: in the one-way example, flying to city 1 in week 0 gives 11 while waiting gives 15.

Maximize Vacation Days With Weekly Directed Flights

hardGoogleSoftware Engineer

You are planning a trip of k weeks across n cities, numbered 0 to n - 1. You start in city 0 on the Monday morning of the first week. Choose where to spend each week so that the total number of vacation days you take is as large as possible.

Flights are given as an n x n matrix flights. flights[i][j] = 1 means there is a direct flight from city i to city j, and flights[i][j] = 0 means there is none. Flights are directed: flights[i][j] = 1 says nothing about flights[j][i]. flights[i][i] is always 0, but staying in your current city is always allowed.

You can take at most one flight per week, and only on Monday morning. Flight time is ignored, so the city you are in after Monday's flight (or after choosing to stay) is where you spend that whole week. On the first Monday you may already fly out of city 0 along any of its flights, or stay.

Vacation limits are given as an n x k matrix days. days[i][j] is the maximum number of vacation days you can take in city i during week j, and week w uses column w of days for w from 0 to k - 1. You may spend a week in a city even when its limit is below 7; the extra days are work days and do not count. If you fly to a city on a Monday, that week's vacation days count toward the destination city.

Implement max_vacation_days(flights, days) so that it returns a single integer: the maximum total number of vacation days you can take over the k weeks. Several schedules may reach that total; only the total is returned. The result is at most 7 * 100 = 700, so it fits in a 32-bit signed integer.

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

Every city has a flight to every other city, so any sequence of cities is possible and the best total is the largest value in each week's column: 6 + 3 + 3 = 12. One such schedule flies to city 1 for week 0, flies to city 2 for week 1, and stays in city 2 for week 2.

Example 2

Input: flights = [[0,1],[0,0]], days = [[2,7,1],[5,0,6]]
Output: 15

The only flight goes from city 0 to city 1, with no way back. Staying in city 0 for weeks 0 and 1 (2 + 7) and flying to city 1 on the third Monday (6) gives 15; the other three possible schedules give 10, 8 and 11.

Constraints

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

Examples

Example 1
Input
([[0]], [[7]])
Output
7
Notes
Minimum shape n = 1, k = 1 with the per-cell maximum 7; the only schedule stays in city 0.
Example 2
Input
([[0]], [[3, 0, 7, 2, 5]])
Output
17
Notes
One city for 5 weeks (row length 5): staying is the only option, so the answer is 3 + 0 + 7 + 2 + 5 = 17.

Constraints

  • •n == flights.length == flights[i].length == days.length
  • •k == days[i].length
  • •1 <= n <= 100
  • •1 <= k <= 100
  • •flights[i][j] is either 0 or 1
  • •flights[i][i] == 0, and staying in the current city is always allowed
  • •0 <= days[i][j] <= 7
  • •The answer is at most 700, so it fits in a 32-bit signed integer

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def max_vacation_days(flights: list[list[int]], days: list[list[int]]) -> int:
Sample call
max_vacation_days([[0]], [[7]])
Language
Case 1
Input
([[0]], [[7]])
Expected
7
Case 2
Input
([[0]], [[3, 0, 7, 2, 5]])
Expected
17

Browse More Questions

Show the approach

Approach

Process the weeks in order and keep one number per city: best[c] is the largest total over all valid schedules that spent the most recent week in city c, or -1 when no valid schedule can be in c yet. Before week 0 the only position is city 0 with total 0. For week w, every city i with best[i] >= 0 may stay in i or fly to any j with flights[i][j] = 1, so nxt[j] is the maximum of best[i] + days[j][w] over i = j and over every reachable i with a flight to j. After week k - 1 the answer is the largest best[c].

Invariant: after week w, best[c] is the maximum vacation total over weeks 0 to w among schedules that spend week w in city c. It holds before week 0, when only city 0 is possible. A schedule that spends week w in city j spent week w - 1 in j or in some city i with a direct flight to j, since only one flight is allowed per week, and swapping its earlier weeks for the best schedule ending in that same city never lowers the total. The recurrence therefore keeps the invariant, and the maximum over all cities after the last week covers every complete schedule.

Edge cases: with n = 1, or with no flights out of city 0, the answer is the sum of row 0. A flight on the first Monday is allowed, so week 0 can count toward a city other than 0. Cities that cannot be reached keep -1 and never contribute even when they hold 7s; since days values of 0 are allowed, -1 is below every reachable total. The answer is at most 700, so 32-bit integers suffice.

Time complexity:
O(k * n^2)
Space complexity:
O(n)