892. Holiday Planner

Hard7.5 points estimatedMatrixDynamic Programming3 hints

A consultant travels between n cities labelled 0 to n-1 over k consecutive weeks, and wants to enjoy as many leisure days as possible. She starts in city 0 before week 0. At the beginning of every week she may stay where she is, or take one direct flight from her current city i to a city j with flights[i][j] == 1 (flights are one-way and take no time; she cannot chain two flights in one week). She then spends the whole week in the city she ends up in and collects days[city][week] leisure days.

Return the maximum total number of leisure days over the k weeks. The diagonal of flights is always 0; staying is always allowed.

Example 1

Input:
flights = [[0,1,0],[0,0,1],[1,0,0]]days = [[1,0,3],[5,0,0],[0,6,2]]
Output:
14
Explanation:

Fly 0->1 in week 0 (5 days), 1->2 in week 1 (6 days), 2->0 in week 2 (3 days): 14 leisure days, which beats staying anywhere.

Example 2

Input:
flights = [[0,0],[0,0]], days = [[2,4,1],[7,7,7]]
Output:
7
Explanation:

No flights exist, so the traveller stays in city 0 all three weeks: 2 + 4 + 1 = 7.

Constraints

  • 1 ≤ n ≤ 100, 1 ≤ k ≤ 100
  • flights is n x n with entries 0 or 1, flights[i][i] == 0
  • days is n x k with 0 ≤ days[i][w] ≤ 7

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,600 msC++ 400 msJava 800 msJavaScript 800 msTypeScript 800 ms

Expected complexity

Time
O(k * n^2)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…