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] = 1 means you can fly from city i to city j
  • πŸ“… days[i][j] represents max vacation days in city i during week j
  • ✈️ 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
Vacation Planning OptimizationCity 0City 1City 2City 3Vacation Days Available Each WeekWeek 0Week 1Week 2Week 3C0:1 C1:6 C2:3C0:3 C1:0 C2:3C0:1 C1:3 C2:3C0:2 C1:1 C2:5Optimal PathMax Days: 12Route: 0β†’1β†’2β†’2
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

n
2n
⚠ Quadratic Growth
Space Complexity
O(n)

We only need to store the DP state for current and next week, which requires O(n) space

n
2n
⚑ 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
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.5K Views
Medium Frequency
~25 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen