HardArrayDynamic ProgrammingMatrix

Maximum Vacation Days

LeetCode
1 approach, code in all languages

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].length
  • n == days.length
  • k == days[i].length
  • 1 <= n, k <= 100
  • flights[i][j] is 0 or 1
  • 0 <= days[i][j] <= 7
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now