Program to find maximum score we can get in jump game in Python

Suppose we have an array called nums and another value k. We are at index 0. In one move, we can jump at most k steps right without going outside the boundaries of the array. We want to reach the final index of the array. For jumping we get score, that is the sum of all nums[j] for each index j we visited in the array. We have to find the maximum score we can get.

So, if the input is like nums = [1,-2,-5,7,-6,4] and k = 2, then the output will be 10 because we jump in this sequence [1, -2, 7, 4], then we will get maximum point, and that is 10.

Algorithm

To solve this, we will follow these steps ?

  • n := size of nums
  • scores := an array of size n and filled with 0
  • scores[0] := nums[0]
  • currMax := scores[0]
  • max_pt := 0
  • if n < 1, then return 0
  • if n is same as 1, then return last element of nums
  • for idx in range 1 to n − 1, do:
    • if max_pt >= idx − k, then update current maximum if needed
    • otherwise, find the maximum score in the valid range
    • scores[idx] = currMax + nums[idx]
  • return last element of scores

Example

Let us see the following implementation to get better understanding ?

def solve(nums, k):
    n = len(nums)
    scores = [0] * n
    scores[0] = nums[0]
    currMax = scores[0]
    max_pt = 0

    if n < 1:
        return 0
    if n == 1:
        return nums[-1]

    for idx in range(1, n):
        if max_pt >= idx - k:
            if currMax < scores[idx-1] and idx > 0:
                currMax = scores[idx-1]
                max_pt = idx-1
        else:
            if idx - k > 0:
                currMax = scores[idx-k]
                max_pt = idx - k
                for p in range(idx-k, idx):
                    if scores[p] >= currMax:
                        max_pt = p
                        currMax = scores[p]
        scores[idx] = currMax + nums[idx]
    
    scores[-1] = currMax + nums[-1]
    return scores[-1]

# Test the function
nums = [1, -2, -5, 7, -6, 4]
k = 2
print(solve(nums, k))

The output of the above code is ?

10

How It Works

The algorithm uses dynamic programming to track the maximum score at each position. For each index, we can jump from any position within the last k steps. We maintain currMax to store the best score we can achieve from valid previous positions, then add the current element to get the score at the current position.

Step-by-Step Trace

For nums = [1, -2, -5, 7, -6, 4] and k = 2:

  • Start at index 0: scores[0] = 1
  • Index 1: Can come from index 0, scores[1] = 1 + (-2) = -1
  • Index 2: Can come from index 0 or 1, choose best: scores[2] = 1 + (-5) = -4
  • Index 3: Can come from index 1 or 2, choose best: scores[3] = 1 + 7 = 8
  • Continue until reaching the end with maximum score of 10

Conclusion

This jump game solution uses dynamic programming to find the maximum score path. The key insight is tracking the best score achievable from valid previous positions within k steps.

Updated on: 2026-03-26T14:16:26+05:30

783 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements