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.
Input: n =2, k =1, stayScore =[[2,3]], travelScore =[[0,2],[1,0]]Output: 3Explanation:
The tourist earns the maximum number of points by starting in city 1 and
staying in that city.
Input: n =3, k =2, stayScore =[[3,4,2],[2,1,2]], travelScore =[[0,2,1],[2,0,4],[3,2,0]]Output: 8Explanation:
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.
At each day and city, the tourist can either stay or move. We want to maximize the total points by making optimal choices at each step. Since the tourist can start at any city, we try all possible starting cities and use dynamic programming to avoid recomputation.
classSolution {
publicintmaximumPoints(int[][] stayScore, int[][] travelScore) {
int k = stayScore.length, n = stayScore[0].length;
int[][] dp =newint[k][n];
for (int j = 0; j < n; ++j) dp[k-1][j]= stayScore[k-1][j];
for (int i = k-2; i >= 0; --i) {
for (int j = 0; j < n; ++j) {
int ans = stayScore[i][j]+ dp[i+1][j];
for (int m = 0; m < n; ++m) {
if (m != j) ans = Math.max(ans, travelScore[j][m]+ dp[i+1][m]);
}
dp[i][j]= ans;
}
}
int res = dp[0][0];
for (int j = 1; j < n; ++j) res = Math.max(res, dp[0][j]);
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funmaximumPoints(stayScore: Array<IntArray>, travelScore: Array<IntArray>): Int {
val k = stayScore.size
val n = stayScore[0].size
val dp = Array(k) { IntArray(n) }
for (j in0 until n) dp[k-1][j] = stayScore[k-1][j]
for (i in k-2 downTo 0) {
for (j in0 until n) {
var ans = stayScore[i][j] + dp[i+1][j]
for (m in0 until n) {
if (m != j) ans = maxOf(ans, travelScore[j][m] + dp[i+1][m])
}
dp[i][j] = ans
}
}
return dp[0].maxOrNull() ?:0 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defmaximumPoints(self, stayScore: list[list[int]], travelScore: list[list[int]]) -> int:
k, n = len(stayScore), len(stayScore[0])
dp: list[list[int]] = [[0]*n for _ in range(k)]
for j in range(n):
dp[k-1][j] = stayScore[k-1][j]
for i in range(k-2, -1, -1):
for j in range(n):
ans = stayScore[i][j] + dp[i+1][j]
for m in range(n):
if m != j:
ans = max(ans, travelScore[j][m] + dp[i+1][m])
dp[i][j] = ans
return max(dp[0])
impl Solution {
pubfnmaximum_points(stay_score: Vec<Vec<i32>>, travel_score: Vec<Vec<i32>>) -> i32 {
let k = stay_score.len();
let n = stay_score[0].len();
letmut dp =vec![vec![0; n]; k];
for j in0..n {
dp[k-1][j] = stay_score[k-1][j];
}
for i in (0..k-1).rev() {
for j in0..n {
letmut ans = stay_score[i][j] + dp[i+1][j];
for m in0..n {
if m != j {
ans = ans.max(travel_score[j][m] + dp[i+1][m]);
}
}
dp[i][j] = ans;
}
}
*dp[0].iter().max().unwrap()
}
}