Maximum Vacation Days - Problem
π΄ Maximum Vacation Days Planning
Imagine you're a lucky employee who gets to travel among n cities over k weeks, collecting algorithm problems while maximizing your vacation days! π―
The Challenge: You start in city 0 on Monday and can only fly between cities on Monday mornings. Each city offers different vacation opportunities each week, and your goal is to plan the optimal travel route to maximize total vacation days.
Travel Rules:
- π«
flights[i][j] = 1means you can fly from cityito cityj - π
days[i][j]represents max vacation days in cityiduring weekj - βοΈ You can only travel on Monday mornings (once per week)
- π¨ You can stay in the same city (no flight needed)
Goal: Return the maximum total vacation days you can achieve across all k weeks!
Input & Output
example_1.py β Basic Travel Planning
$
Input:
flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[1,3,1],[6,0,3],[3,3,3]]
βΊ
Output:
12
π‘ Note:
Optimal path: Stay in city 0 for week 0 (1 vacation day), fly to city 1 for week 1 (0 vacation days), fly to city 2 for week 2 (3 vacation days). Total = 1 + 0 + 3 = 4. Actually, better path: Fly to city 1 in week 0 (6 days), stay in city 1 for week 1 (0 days), fly to city 2 for week 2 (3 days). Total = 6 + 0 + 3 = 9. Even better: City 0 week 0 (1), City 1 week 1 (0), City 2 week 2 (3), but we can also go City 1 week 0 (6), City 2 week 1 (3), City 2 week 2 (3) = 12.
example_2.py β No Flights Available
$
Input:
flights = [[0,0,0],[0,0,0],[0,0,0]], days = [[1,1,1],[7,7,7],[7,7,7]]
βΊ
Output:
3
π‘ Note:
Since no flights are available (all flights[i][j] = 0), we must stay in city 0 for all weeks. Total vacation days = 1 + 1 + 1 = 3.
example_3.py β Single City
$
Input:
flights = [[0]], days = [[5,2,8,3]]
βΊ
Output:
18
π‘ Note:
Only one city available. Stay in city 0 for all 4 weeks. Total vacation days = 5 + 2 + 8 + 3 = 18.
Visualization
Tap to expand
Understanding the Visualization
1
Setup Initial State
Start at city 0 with 0 vacation days accumulated
2
Week-by-Week Planning
For each week, consider staying in current city or flying to connected cities
3
Track Maximum Gains
Keep track of the maximum vacation days achievable at each city for current week
4
Build Optimal Solution
Use previous week's results to compute current week's optimal choices
Key Takeaway
π― Key Insight: Dynamic programming builds the optimal solution incrementally by considering all possible travel choices each week while remembering the best outcomes from previous weeks.
Time & Space Complexity
Time Complexity
O(nΒ²k)
For each of k weeks, we check n cities, and for each city we check n possible destinations
β Quadratic Growth
Space Complexity
O(n)
We only need to store the DP state for current and next week, which requires O(n) space
β‘ Linearithmic Space
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
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code