Ctrl K

Weeks of Rest by Flight

Hard
A company lets an employee spend k weeks moving between n offices in different cities (numbered 0 to n - 1). On the Monday of the first week the employee is at office 0. The n × n matrix routes describes flights: routes[i][j] = 1 if there is a direct flight from city i to city j, otherwise 0 (no city has a flight to itself, and a return flight is not guaranteed). Flights are allowed only on Monday morning, at most one per Monday, or the employee can stay put. A flight takes no time. The n × k matrix rest says rest[i][w] is how many days the employee can rest in city i during week w. Rest counts toward the city where the week is spent (after a Monday flight, the whole week counts for the new city). The other days of the week are work days. Return the largest total number of rest days over the k weeks.
Example 1
routes=[[0, 1, 0], [0, 0, 1], [1, 0, 0]], rest=[[2, 1, 0], [5, 0, 4], [0, 6, 7]] → 18

Week 1: fly to office 1 and rest 5 days. Week 2: fly to office 2 and rest 6. Week 3: stay there for another 7. That is 18 in total.

Example 2
routes=[[0, 0], [0, 0]], rest=[[3, 2], [7, 7]] → 5

There are no flights, so every week is spent in office 0: 3 + 2 = 5.

Constraints
  • n = len(routes) = len(routes[i]) = len(rest)
  • k = len(rest[i])
  • 1 ≤ n, k ≤ 100
  • routes[i][j] is 0 or 1, routes[i][i] = 0
  • 0 ≤ rest[i][w] ≤ 7
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Solid Panel Block HardCrossing the Cave HardLongest Uphill Walk HardBerry Field Round Trip HardSix-Cell Tile Slider Hard
loading Python…
Input
routes=[[0, 1, 0], [0, 0, 1], [1, 0, 0]], rest=[[2, 1, 0], [5, 0, 4], [0, 6, 7]]
Expected
18

Run checks the first 3 tests in your browser. Submit checks all 63 tests on the server, hidden ones included.