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 <= 1031 <= nums[i] <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are standing in a line of numbered tiles and you want to hop your way forward while collecting the maximum possible points. The brute force approach works by stubbornly trying out every single combination of hops possible from start to finish.
Here's how the algorithm would work step-by-step:
def max_score_brute_force(tiles):
# We define a recursive helper to explore every possible hopping path and calculate all scores
def explore_paths(current_index, current_score):
if current_index >= len(tiles) - 1:
return current_score
maximum_total_score = 0
# We iterate through all subsequent tiles to try every possible branch of the decision tree
for next_tile_index in range(current_index + 1, len(tiles)):
hop_score = (next_tile_index - current_index) * tiles[next_tile_index]
total_path_score = explore_paths(next_tile_index, current_score + hop_score)
if total_path_score > maximum_total_score:
maximum_total_score = total_path_score
return maximum_total_score
# We kick off the exhaustive search starting from the very first tile with an initial score of zero
return explore_paths(0, 0)
The problem asks for the maximum score to reach the last element by hopping forward, where each hop from index i to j awards (j - i) * nums[j]. Each unit step between index 0 and the final index contributes nums[j] to the total score, where j is the next hop landing point. Thus, for each unit interval, we greedily pick the maximum element that lies to its right.
Here's how the algorithm would work step-by-step:
def max_score(nums: list[int]) -> int:
total_score = 0
current_max = 0
# Iterate backwards through the array to find the optimal hop target.
for index in range(len(nums) - 1, 0, -1):
# Keep track of the suffix maximum value seen so far.
if nums[index] > current_max:
current_max = nums[index]
# Add the maximum reachable multiplier for each unit step.
total_score += current_max
return total_score
| Case | How to Handle |
|---|---|
| Empty or null input array | Check for null or empty arrays at the start and return a score of zero immediately. |
| Single element array | Return a score of zero since no hops can be made from the starting position to a subsequent position. |
| Array with only two elements | Compute the direct score from the first element to the second element and return it as the only option. |
| Array containing zeros at intermediate positions | The algorithm should correctly calculate hop distances and scores even when landing on or jumping from zero values. |
| Strictly decreasing array values | Ensure the dynamic programming or greedy approach properly evaluates all intermediate hops to find the optimal path. |
| Extremely large array size causing performance bottlenecks | Implement an O(n) or O(n squared) solution that avoids deep recursion and optimizes state transitions to scale efficiently. |
| Large integer values causing arithmetic overflow | Use 64-bit integer types for score accumulation to prevent overflow during cumulative additions. |
| All identical values across the array | The algorithm naturally evaluates skipping intermediate steps versus taking consecutive steps to maximize the total score. |