You are given two integers, n and k, along with two 2D integer arrays, stayScore and travelScore.
A tourist is visiting a country with n cities, where each city is directly connected to every other city. The tourist's journey consists of exactlyk0-indexed days, and they can choose any city as their starting point.
Each day, the tourist has two choices:
Stay in the current city: If the tourist stays in their current city curr during day i, they will earn stayScore[i][curr] points.
Move to another city: If the tourist moves from their current city curr to city dest, they will earn travelScore[curr][dest] points.
Return the maximum possible points the tourist can earn.
The tourist earns the maximum number of points by starting in city 1, staying in that city on day 0, and traveling to city 2 on day 1.
Constraints:
1 <= n <= 200
1 <= k <= 200
n == travelScore.length == travelScore[i].length == stayScore[i].length
k == stayScore.length
1 <= stayScore[i][j] <= 100
0 <= travelScore[i][j] <= 100
travelScore[i][i] == 0
Solutions
Solution 1
Thinking
Over \(k\) days we may stay or travel, scoring stay points or travel points. With \(n,k \le 200\) the state space is \(O(nk)\) and each transition enumerates the previous city.
\(f[i][j]\) is the best score after day \(i\) in city \(j\). Staying from \(h=j\) adds \(\textit{stayScore}[i-1][j]\); travelling from \(h\) adds \(\textit{travelScore}[h][j]\).
Day \(0\) has score \(0\) in every city. The answer is the maximum of \(f[k]\).