anandkumar.kurapati
BAN USERThe time complexity of your programme is O(n^2) and it does not gives correct answer for negative sums
- anandkumar.kurapati August 29, 2013// Returns minimum number of jumps to reach arr[n-1] from arr[0]
int minJumps(int arr[], int n)
{
int *jumps = new int[n]; // jumps[n-1] will hold the result
int i, j;
if (n == 0 || arr[0] == 0)
return INT_MAX;
jumps[0] = 0;
// Find the minimum number of jumps to reach arr[i]
// from arr[0], and assign this value to jumps[i]
for (i = 1; i < n; i++)
{
jumps[i] = INT_MAX;
for (j = 0; j < i; j++)
{
if (i <= j + arr[j] && jumps[j] != INT_MAX)
{
jumps[i] = jumps[j] + 1;
break;
}
}
}
return jumps[n-1];
}
1
/
2
/ \
3 4
\
5
5 also prints in this case ,as it is leftmost of that level
}
- anandkumar.kurapati August 31, 2013