[LeetCode] Reach End of Array With Max Score

3282. Reach End of Array With Max Score

You are given an integer array nums of length n.

Your goal is to start at index 0 and reach index n - 1. You can only jump to indices greater than your current index.

The score for a jump from index i to index j is calculated as (j - i) * nums[i].

Return the maximum possible total score by the time you reach the last index.

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