Mid/SeniorArrayGreedy

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.

Example 1
Inputnums = [1,3,1,5]
Output7
First, jump to index 1 and then jump to the last index, giving a final score of 1 * 1 + 2 * 3 = 7.
Example 2
Inputnums = [4,3,1,3,2]
Output16
Jumping directly to the last index gives a final score of 4 * 4 = 16.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate