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 n cities, represented by indexes from 0 to n - 1.
  • Initially, you are in the city indexed 0 on Monday.
  • The cities are connected by flights. The flights are represented as an n x n matrix (not necessarily symmetrical), called flights where flights[i][j] == 1 means there is a flight from city i to city j, otherwise flights[i][j] == 0.
  • You totally have k weeks 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 k matrix called days where days[i][j] represents the maximum days you could take a vacation in city i in week j.

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
Maximum Vacation Days: Travel OptimizationCity 0STARTCity 16,0,3City 24,2,2Vacation DaysWeek 0: 1,3,1Week 1: 6,0,3Week 2: 4,2,2Optimal: 12 daysFind the travel schedule that maximizes vacation daysConsider flight connections and weekly vacation limits per city
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.
Asked in
Google 15 Amazon 12 Facebook 8 Microsoft 6
15.6K Views
Medium Frequency
~35 min Avg. Time
542 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