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 - DP Solution INPUT 3 Cities (n=3), 3 Weeks (k=3) C0 C1 C2 All cities connected flights matrix: [0,1,1] [1,0,1] [1,1,0] days matrix: W0 W1 W2 C0: [1, 3, 1] C1: [6, 0, 3] C2: [4, 2, 2] ALGORITHM STEPS 1 Initialize DP Table dp[city][week] = max days 2 Process Week by Week For each week, try all cities 3 Check Flight Options Stay or fly if connected 4 Update Maximum dp[j][w] = max(prev + days) DP Table Evolution: City W0 W1 W2 C0 1 9 10 C1 6 6 12 C2 4 8 11 Optimal: C0-->C1-->C0-->C1 Days: 6 + 3 + 3 = 12 W0:Stay C1(6) W1:Fly C0(3) W2:Fly C1(3) FINAL RESULT Maximum Vacation Days 12 Output: 12 days Optimal Schedule: W0 City 1 +6 W1 City 0 +3 W2 City 1 +3 6 + 3 + 3 = 12 OK - Verified! Time: O(n^2 * k) Space: O(n * k) Key Insight: Use Dynamic Programming where dp[city][week] represents the maximum vacation days achievable ending at that city in that week. For each week, consider staying in current city or flying to any connected city. The answer is max(dp[all cities][last week]). Start from city 0 in week 0. TutorialsPoint - Maximum Vacation Days | Dynamic Programming Approach
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