You are given a 0-indexed integer array nums and an integer k.
You are initially standing at index 0. In one move, you can jump at most k steps forward without going outside the boundaries of the array. That is, you can jump from index i to any index in the range [i + 1, min(n - 1, i + k)] inclusive.
You want to reach the last index of the array (index n - 1). Your score is the sum of all nums[j] for each index j you visited in the array.
Return the maximum score you can get.
Example 1:
Input: nums = [1,-1,-2,4,-7,3], k = 2 Output: 7 Explanation: You can choose your jumps forming the subsequence [1,-1,4,3] (underlined above). The sum is 7.
Example 2:
Input: nums = [10,-5,-2,4,0,3], k = 3 Output: 17 Explanation: You can choose your jumps forming the subsequence [10,4,3] (underlined above). The sum is 17.
Example 3:
Input: nums = [1,-5,-20,4,-1,3,-6,-3], k = 2 Output: 0
Constraints:
1 <= nums.length, k <= 105-104 <= nums[i] <= 104When 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:
The goal is to reach the end, making the best score possible. The brute force method explores every path, jumping different lengths to see where we end up.
Here's how the algorithm would work step-by-step:
def jump_game_vi_brute_force(numbers, max_jump):
def find_max_score(current_index):
# If we've reached the end, return the score.
if current_index == len(numbers) - 1:
return numbers[current_index]
max_score = float('-inf')
# Iterate through all possible jump lengths.
for jump_length in range(1, min(max_jump + 1, len(numbers) - current_index)):
next_index = current_index + jump_length
# Recursively call find_max_score from the next position.
score = numbers[current_index] + find_max_score(next_index)
max_score = max(max_score, score)
return max_score
# Start the recursion from the first index
return find_max_score(0)The best approach isn't about trying every possible jump. It's about smartly keeping track of the best possible score we can reach at each position, and using that to efficiently find the best final score. We maintain a 'window' of the best previous scores, allowing us to quickly decide the next best jump.
Here's how the algorithm would work step-by-step:
from collections import deque
def jump_game_vi(numbers, max_jump):
score = [0] * len(numbers)
score[0] = numbers[0]
window = deque([0])
for i in range(1, len(numbers)):
# Keep the queue within the max jump range
while window and window[0] < i - max_jump:
window.popleft()
# The best score at the current position.
score[i] = score[window[0]] + numbers[i]
# Maintain the window's relevance by removing
# worse scores that are further behind.
while window and score[window[-1]] <= score[i]:
window.pop()
window.append(i)
return score[-1]| Case | How to Handle |
|---|---|
| Empty input array | Return 0, as no jumps are possible and the problem statement implies the array will always be non-empty. |
| Array with a single element | Return the value of the single element, as no jumps are needed. |
| k = 0 | If k is 0, we can only jump to the current position; therefore, the result is simply the first element if the array's length is 1. |
| All numbers are negative | The algorithm should correctly select the maximum score within the allowed jump range, even if all values are negative. |
| The first element is very large, and other elements are small or negative | Ensure integer overflow does not occur when calculating the cumulative score. |
| k is greater than or equal to the length of the array | Handle this case by treating k as array.length - 1, as we can jump to the end in a single step. |
| Large input array with large k | The solution needs to scale efficiently, avoiding naive O(n*k) approaches that would lead to timeouts; prioritize a heap or deque-based solution. |
| Array with only zeros after the first element. | The algorithm should still find the path to the end from the first element even if subsequent jumps yield no gain in score. |