I have been trying the Bytelandian gold coins problem but my code runs with “Time limit exceeded” result. I am a little less intimate with recursion. Can anyone help me out.
My code is as follows:
#define MAX(a,b) a>b?a:b
unsigned long int max=0;
int findMax(unsigned long int n)
{
if(n<12)
return n;
else
{
max=findMax(n/2)+findMax(n/3)+findMax(n/4);
return MAX(max,n);
}
}
int main()
{
unsigned long int n;
while(scanf("%lu",&n)!=EOF)
{
printf("%lu\n",findMax(n));
}
return 0;
}
apri 2017 calendar april calendar 2017
but my code runs with “Time limit exceeded” result. I am a little less intimate with recursion. Can anyone help me out
It is obvious that you will require findMax(n) for smaller values of n. Now, when you are calculating the findMax(n) you can make slight modification to your code such that if you have already solved a particular value, you don’t need to solve it again.
For eg.
solve(int n)
{
if (alreadysolved[n])
return alreadysolved[n];
/*
Your code…
solve()
*/
}