Maximum Vacation Days Problem
Description
LeetCode Problem 568.
LeetCode wants to give one of its best employees the option to travel among n cities to collect algorithm problems. But all work and no play makes Jack a dull boy, you could take vacations in some particular cities and weeks. Your job is to schedule the traveling to maximize the number of vacation days you could take, but there are certain rules and restrictions you need to follow.
Rules and restrictions:
- You can only travel among
ncities, represented by indexes from0ton - 1. Initially, you are in the city indexed0on Monday. - The cities are connected by flights. The flights are represented as an
n x nmatrix (not necessarily symmetrical), calledflightsrepresenting the airline status from the cityito the cityj. If there is no flight from the cityito the cityj,flights[i][j] == 0; Otherwise,flights[i][j] == 1. Also,flights[i][i] == 0for alli. - You totally have
kweeks (each week has seven days) to travel. You can only take flights at most once per day and can only take flights on each week's Monday morning. Since flight time is so short, we do not consider the impact of flight time. - For each city, you can only have restricted vacation days in different weeks, given an
n x kmatrix calleddaysrepresenting this relationship. For the value ofdays[i][j], it represents the maximum days you could take a vacation in the cityiin the weekj. - You could stay in a city beyond the number of vacation days, but you should work on the extra days, which will not be counted as vacation days.
- If you fly from city
Ato cityBand take the vacation on that day, the deduction towards vacation days will count towards the vacation days of cityBin that week. - We do not consider the impact of flight hours on the calculation of vacation days.
Given the two matrices flights and days, return the maximum vacation days you could take during k weeks.
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, we 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.lengthn == flights[i].lengthn == days.lengthk == days[i].length1 <= n, k <= 100flights[i][j]is either0or1.0 <= days[i][j] <= 7
Solution
Approach: Dynamic Programming over Weeks
Each week is spent in a single city: on Monday we either stay where we are or take one flight, then we enjoy that city's vacation days for the week. What happens later only depends on which city we are in, not on how we got there, so we can keep, for every city, the best total so far.
Let best[j] be the most vacation days we can have collected when the current week is spent in city j, or -1 if we cannot be in city j that week. Before the first week we are in city 0, so best[0] = 0 and every other city is -1.
For each week, and each city j, we can arrive in j by staying there (best[j]) or by flying from some city i with flights[i][j] == 1 (best[i]). Take the largest of these that is reachable and add days[j][week]. After the last week, the answer is the largest value in best.
The -1 marks matter: a city with many vacation days that we cannot reach yet must not be used as a starting point. Note too that the first Monday also allows a flight, as example 1 shows, which the recurrence handles because week 0 is computed like every other week.
Complexity: O(k·n2) time, since each of the k weeks checks every pair of cities, and O(n) extra space.
class Solution:
def maxVacationDays(self, flights: List[List[int]], days: List[List[int]]) -> int:
n, k = len(flights), len(days[0])
# best[j]: most vacation days so far when this week is spent in city j (-1: city j unreachable).
best = [-1] * n
best[0] = 0
for week in range(k):
nxt = [-1] * n
for j in range(n):
# Stay in j, or fly to j from any reachable city i.
came = best[j]
for i in range(n):
if flights[i][j] and best[i] > came:
came = best[i]
if came >= 0:
nxt[j] = came + days[j][week]
best = nxt
return max(best)