Talentd
FleetCode

3205. Maximum Array Hopping Score I

Medium77.0% acceptance1 companyPremium elsewhere, free here

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.

Example 1:

Input: nums = [1,5,8]

Output: 16

Explanation:

There are two possible ways to reach the last element:

  • 0 -> 1 -> 2 with a score of (1 - 0) * 5 + (2 - 1) * 8 = 13.
  • 0 -> 2 with a score of (2 - 0) * 8 = 16.

Example 2:

Input: nums = [4,5,2,8,9,1,3]

Output: 42

Explanation:

We can do the hopping 0 -> 4 -> 6 with a score of (4 - 0) * 9 + (6 - 4) * 3 = 42.

Constraints:

  • 2 <= nums.length <= 103
  • 1 <= nums[i] <= 105

Asked at 1 company

Hints open one at a time

Hint 1
Define dp[i] as the maximum score if we start from index i.
Hint 2
We can calculate dp[i] as the maximum score from hopping to all indices j > i.
14:14
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[1,5,8]
Input
[4,5,2,8,9,1,3]
Your code is saved on this device.Solution