- 2
Input: N=11 arr=1 3 5 8 9 2 6 7 6 8 9 Output: 3
Given an array of integers where each element represents the max number of steps that can be made forward from that element. Find the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then you cannot move through that element.
3 Respuestas
+ 1
How does it work?
And please mention the language name that is relevant with your question in tags. Also post your attempt so we can see what you have tried so far.
0
if (n <= 1):
return 0
if (arr[0] == 0):
return -1
maxReach = arr[0]
step = arr[0]
jump = 1
for i in range(1, n):
if (i == n-1):
return jump
maxReach = max(maxReach, i + arr[i])
step -= 1;
if (step == 0):
jump += 1
if(i >= maxReach):
return -1
step = maxReach - i;
return -1
0
Seems that your is in python.. code result depends on the indentation, how you add. So pls add your total code link which you are trying with saving in playground.. otherwise its very difficult to find mistakes if any..
Also you need add some more description that " is it need to find from every element or from starting element only.. for later, you get only one way , 1-3-9-9 then why need to minimum number of jumps? and how?
For every element, that to big doubt 9->9 is single step always..
Pls Add full details clearly ...