Maximum Vacation Days
1 approach, code in every language
Problem description
There are n cities and k weeks of vacation to plan. A matrix flights of size n x n describes travel: flights[i][j] = 1 means you may fly from city i to city j, and staying put in the same city is always allowed (treat i -> i as permitted regardless of the flights entry).
A second matrix days of size n x k tells you how many vacation days you can take: days[i][w] is the number of days available in city i during week w, capped by that city's rules. On the first day of each week you may either stay or take one flight to another reachable city, and you spend the remainder of that week in whichever city you land in.
You begin in city 0 at the start of week 0. Return the maximum total vacation days you can accumulate across all k weeks.
Example 1
Input: flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[1,3,1],[6,0,3],[3,3,3]]
Output: 12
Week 0 fly to city 1 (6 days), week 1 fly to city 2 (3 days), week 2 fly to city 1 (3 days): 6 + 3 + 3 = 12.
Example 2
Input: flights = [[0,0,0],[0,0,0],[0,0,0]], days = [[1,1,1],[7,7,7],[7,7,7]]
Output: 3
No flights exist, so you are stuck in city 0 the whole time, collecting 1 + 1 + 1 = 3 days.
Constraints
n == flights.length == flights[i].lengthn == days.lengthk == days[i].length1 <= n, k <= 100flights[i][j] is 0 or 10 <= days[i][j] <= 7
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Reading a pattern isn't the same as using it. The practice set has 1,194 original problems on the same techniques, each graded in your browser.
1,194 problems · 15,062 graded tests · free