Skip to content

Instantly share code, notes, and snippets.

@rohit-nsit08
Created August 13, 2011 15:58
Show Gist options
  • Select an option

  • Save rohit-nsit08/1143982 to your computer and use it in GitHub Desktop.

Select an option

Save rohit-nsit08/1143982 to your computer and use it in GitHub Desktop.
hopping using greedy approach
#include<stdio.h>
int main()
{
int arr[] = {1, 3, 5, 8, 9, 1,1,2, 6, 7, 6, 8};
int n = sizeof(arr)/sizeof(int);
int i,j,step=0,jump=0,choice,max,val;
for(i=0;i<n;)
{
choice = arr[i];
max = 0;
while(choice)
{
if((i+choice)>(n-1)){--choice;continue;}
val = arr[i+choice];
if(val >max){max = val;step = choice;}
choice--;
}
if(arr[i]+i>=n){
jump++;
break;
}
jump++;
i = i+step;
}
printf("%d",jump);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment