Plan weekly flights between cities to maximize total vacation days
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Other
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.
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
- 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.
- A city with large values may be unreachable, or reachable only after several weeks of single flights.
- 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.