-
Notifications
You must be signed in to change notification settings - Fork 17
/
minJumps.cpp
70 lines (52 loc) · 1.55 KB
/
minJumps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Given an array of N integers arr[] where each element represents the max length of the jump 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.
#include <iostream>
using namespace std;
int minJumps(int arr[], int n)
{
if (n <= 1)
return 0;
if (arr[0] == 0)
return -1;
int jump = 1;
int subArrEndIndex = arr[0];
int i = 1;
int subArrFistHalfMaxSteps = 0;
int subArrSecondHalfMaxSteps = 0;
// Start traversing array
for (i = 1; i < n;)
{
subArrEndIndex = i + subArrEndIndex;
if (subArrEndIndex >= n)
return jump;
int firstHalfMaxStepIndex = 0;
for (; i < subArrEndIndex; i++)
{
int stepsCanCover = arr[i] + i;
if (subArrFistHalfMaxSteps < stepsCanCover)
{
subArrFistHalfMaxSteps = stepsCanCover;
subArrSecondHalfMaxSteps = 0;
firstHalfMaxStepIndex = i;
}
else if (subArrSecondHalfMaxSteps < stepsCanCover)
{
subArrSecondHalfMaxSteps = stepsCanCover;
}
}
if (i > subArrFistHalfMaxSteps)
return -1;
jump++;
subArrEndIndex = arr[firstHalfMaxStepIndex];
subArrFistHalfMaxSteps = subArrSecondHalfMaxSteps;
}
return -1;
}
// Driver program to test above function
int main()
{
int arr[] = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9};
int size = sizeof(arr) / sizeof(int);
cout << ("Minimum number of jumps to reach end is %d ",
minJumps(arr, size));
return 0;
}