Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save rohit-nsit08/1143909 to your computer and use it in GitHub Desktop.
hopping using dynamic programming
#include<stdio.h>
int main()
{
int input[] = {1, 3, 5 ,8 ,9 ,2 ,6, 7, 6, 8, 9};
int n = sizeof(input)/sizeof(int);
int dp[n];
int i,j,min,value,x;
dp[n-1] = 0;
for(i=n-2;i>=0;i--)
{
min =32765; // any large value will do
for(j=1;(j<=input[i])&&((i+j)<n);j++)
{
value =dp[i+j];
if((value+1)<=min)min = dp[i+j]+1;
}
dp[i]=min;
}
printf("%d",dp[0]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment