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.
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.
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.
Input: nums = [1,-5,-20,4,-1,3,-6,-3], k = 2
Output: 0
In the brute force approach, the idea is to explore every possible path by jumping up to k steps forward and choose the path that yields the maximum score. This involves recursively jumping from the current position all the way to the n-1 index and calculating the score for each path.
k further recursive calls.n.To achieve better performance, we utilize a deque to maintain a sliding window of size k over the array and store indices. The front of the deque will always represent the maximum possible score we can get at that point. This allows calculating the maximum score at each step in constant time.
This approach uses a priority queue to maintain the maximum scores at each index similar to the deque strategy. The priority queue will always allow us to peek the current maximum score in logarithmic time.