dp
with dimensions n x (k+1)
and set all values to 0.i
from n-1
to 0
, fill dp[i][k]
with days[i][k]
for the last week k
.k
from k-1
to 0
, iterate over each city i
:
a. Set dp[i][k]
to days[i][k] + dp[i][k+1]
to represent staying in the same city.
b. Iterate over each city j
and check if a flight exists from i
to j
(flights[i][j] == 1
).
c. If a flight exists, calculate the vacation days for flying to city j
: days[j][k] + dp[j][k+1]
.
d. Update dp[i][k]
with the maximum of its current value and the value calculated for flying to city j
.dp[0][0]
, which represents the maximum vacation days starting from city 0 in week 0.