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

PythonServer Side ProgrammingProgramming

<p>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.</p><p>So, if the input is like nums = [1,-2,-5,7,-6,4] 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.</p><p>To solve this, we will follow these steps &minus;</p><ul class="list"><li>n := size of nums</li><li>scores := an array of size n and filled with 0</li><li>scores[0] := nums[0]</li><li>currMax := scores[0]</li><li>max_pt := 0</li><li>if n &lt; 1, then<ul class="list"><li>return 0</li></ul></li><li>if n is same as 1, then<ul class="list"><li>return last element of nums</li></ul></li><li>for idx in range 1 to n - 1, do<ul class="list"><li>if max_pt &gt;= idx - k, then<ul class="list"><li>if currMax &lt; scores[idx-1] and idx &gt; 0, then<ul class="list"><li>currMax := scores[idx-1]</li><li>max_pt := idx-1</li></ul></li></ul></li><li>otherwise,<ul class="list"><li>if idx - k &gt; 0, then<ul class="list"><li>currMax := scores[idx-k]</li><li>max_pt := idx - k</li><li>for p in range idx-k to idx, do<ul class="list"><li>if scores[p] &gt;= currMax, then<ul class="list"><li>max_pt := p</li><li>currMax := scores[p]</li></ul></li></ul></li></ul></li></ul></li><li>scores[idx] := currMax + nums[idx]</li></ul></li><li>last element of scores := currMax + nums[-1]</li><li>return last element of scores</li></ul><h2>Example</h2><p>Let us see the following implementation to get better understanding &minus;</p><pre class="demo-code notranslate language-python" data-lang="python"> def solve(nums, k): &nbsp; &nbsp;n = len(nums) &nbsp; &nbsp;scores = [0] * n &nbsp; &nbsp;scores[0] = nums[0] &nbsp; &nbsp;currMax = scores[0] &nbsp; &nbsp;max_pt = 0 &nbsp; &nbsp;if n &lt; 1: &nbsp; &nbsp; &nbsp; return 0 &nbsp; &nbsp;if n == 1: &nbsp; &nbsp; &nbsp; return nums[-1] &nbsp; &nbsp;for idx in range(1,n): &nbsp; &nbsp; &nbsp; if max_pt &gt;= idx - k: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if currMax &lt; scores[idx-1] and idx &gt; 0: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currMax = scores[idx-1] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max_pt = idx-1 &nbsp; &nbsp; &nbsp; else: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if idx - k &gt; 0: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currMax = scores[idx-k] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max_pt = idx - k &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for p in range(idx-k, idx): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if scores[p] &gt;= currMax: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max_pt = p &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currMax = scores[p] &nbsp; &nbsp; &nbsp; scores[idx] = currMax + nums[idx] &nbsp; &nbsp;scores[-1] = currMax + nums[-1] &nbsp; &nbsp;return scores[-1] nums = [1,-2,-5,7,-6,4] k = 2 print(solve(nums, k))</pre><h2>Input</h2><pre class="result notranslate">[1,-2,-5,7,-6,4], 2 </pre><h2>Output</h2><pre class="result notranslate">10</pre>
Updated on 06-Oct-2021 11:21:46