Maximum Vacation Days - Problem
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 indexed
0on Monday. - The cities are connected by flights. The flights are represented as an
n x nmatrix (not necessarily symmetrical), calledflightswhereflights[i][j] == 1means there is a flight from cityito cityj, otherwiseflights[i][j] == 0. - You totally have
kweeks to travel. You can only take flights at most once per day and can only take flights on each week's Monday morning. - For each city, you can only have restricted vacation days in different weeks, given an
n x kmatrix calleddayswheredays[i][j]represents the maximum days you could take a vacation in cityiin weekj.
Input & Output
Example 1 — Basic Case
$
Input:
flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[1,3,1],[6,0,3],[4,2,2]]
›
Output:
12
💡 Note:
Optimal path: Week 0: fly to city 1 (6 days), Week 1: stay in city 1 (0 days), Week 2: fly to city 2 (2 days). Total: 6 + 0 + 2 = 8. Actually, better path: Week 0: fly to city 1 (6 days), Week 1: fly to city 2 (2 days), Week 2: stay in city 2 (2 days). Total: 6 + 2 + 2 = 10. Best path: Week 0: fly to city 1 (6 days), Week 1: fly to city 2 (2 days), Week 2: fly to city 1 (3 days) or Week 0: fly to city 2 (4 days), Week 1: fly to city 1 (0 days), Week 2: fly to city 1 (3 days) = 7. The actual maximum is 12.
Example 2 — No Flights Available
$
Input:
flights = [[0,0,0],[0,0,0],[0,0,0]], days = [[1,1,1],[7,7,7],[6,6,6]]
›
Output:
3
💡 Note:
No flights available, must stay in city 0 for all 3 weeks: 1 + 1 + 1 = 3 vacation days
Example 3 — All Cities Connected
$
Input:
flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[2,5,1],[4,1,3],[3,2,4]]
›
Output:
13
💡 Note:
Optimal: Week 0: fly to city 1 (4 days), Week 1: stay in city 1 (1 day), Week 2: fly to city 2 (4 days). Total: 4 + 1 + 4 = 9. Better: Week 0: fly to city 1 (4 days), Week 1: fly to city 0 (5 days), Week 2: fly to city 2 (4 days) = 13
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] ≤ 103
- flights[i][i] == 0 for all i
Visualization
Tap to expand
Understanding the Visualization
1
Input
Flight connections matrix and vacation days per city-week
2
Process
Use DP to find optimal travel schedule
3
Output
Maximum vacation days achievable
Key Takeaway
🎯 Key Insight: Use dynamic programming to track maximum vacation days achievable from each city-week combination, building the solution backwards from the final week.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code