HardNo. 0568LeetCode Premium
Maximum Vacation Days — LeetCode 568 Python Solution
- Pattern
- Matrix and Grid
- Topics
- Array, Dynamic Programming, Matrix
- Reading time
- 3 min
- Source
- leetcode.com
The 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.
Example
- Input
- flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[1,3,1],[6,0,3],[3,3,3]]
- Output
- 12
- Explanation
- One of the best strategies is:
Python solution
class Solution:
def maxVacationDays(self, flights: List[List[int]], days: List[List[int]]) -> int:
n = len(flights)
K = len(days[0])
f = [[-inf] * n for _ in range(K + 1)]
f[0][0] = 0
for k in range(1, K + 1):
for j in range(n):
f[k][j] = f[k - 1][j]
for i in range(n):
if flights[i][j]:
f[k][j] = max(f[k][j], f[k - 1][i])
f[k][j] += days[j][k - 1]
return max(f[-1][j] for j in range(n))Complexity
- Time
- O(n·m) (typical)
- Space · auxiliary
- O(n·m)or optimized
Pattern: Matrix and Grid
Treat a 2-D grid as a graph whose neighbours are the four adjacent cells. LeetCode 568. Maximum Vacation Days is filed here because LeetCode tags it Matrix, which is the vocabulary this hub collects.
The matrix and grid guide has the Python template for the pattern and the 216 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0063 | Unique Paths II | Medium |
| 0064 | Minimum Path Sum | Medium |
| 0085 | Maximal Rectangle | Hard |
| 0174 | Dungeon Game | Hard |
| 0221 | Maximal Square | Medium |
| 0329 | Longest Increasing Path in a Matrix | Hard |
Frequently asked questions
- How hard is LeetCode 568. Maximum Vacation Days?
LeetCode 568. Maximum Vacation Days is rated Hard on LeetCode.
- What topics does LeetCode 568. Maximum Vacation Days cover?
LeetCode 568. Maximum Vacation Days is tagged Array, Dynamic Programming and Matrix on LeetCode.
- Is LeetCode 568. Maximum Vacation Days a premium problem?
Yes. LeetCode 568. Maximum Vacation Days is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.