[LeetCode] Maximum Array Hopping Score I

3205. Maximum Array Hopping Score I

Given an array nums, you have to get the maximum score starting from index 0 and hopping until you reach the last element of the array.

In each hop, you can jump from index i to an index j > i, and you get a score of (j - i) * nums[j].

Return the maximum score you can get.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int maxScore(vector<int>& A) {
reverse(begin(A), end(A));
int res = 0, ma = 0;
for(int i = 0; i < A.size() - 1; i++) {
ma = max(ma, A[i]);
res += ma;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/07/07/PS/LeetCode/maximum-array-hopping-score-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.