Skip to content

3221. Maximum Array Hopping Score II 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
 public:
  // Same as 3205. Maximum Array Hopping Score I
  long long maxScore(vector<int>& nums) {
    // The optimal jump is the maximum number in the remaining suffix.
    long ans = 0;
    int mx = 0;

    for (int i = nums.size() - 1; i > 0; --i) {
      mx = max(mx, nums[i]);
      ans += mx;
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
  // Same as 3205. Maximum Array Hopping Score I
  public long maxScore(int[] nums) {
    // The optimal jump is the maximum number in the remaining suffix.
    long ans = 0;
    int mx = 0;

    for (int i = nums.length - 1; i > 0; --i) {
      mx = Math.max(mx, nums[i]);
      ans += mx;
    }

    return ans;
  }
}
1
2
3
4
5
class Solution:
  # Same as 3205. Maximum Array Hopping Score I
  def maxScore(self, nums: list[int]) -> int:
    # The optimal jump is the maximum number in the remaining suffix.
    return sum(itertools.accumulate(nums[:0:-1], max))