+ 3

Code coatch [Hofstadter's Q-Sequence]

I made a program that is giving correct output for test case 1and 2 but failing test case 3-6. Test cases 3-6 are hidden so I don't know in which case it is failing. How can I correct it if I don't know what the problem is?

1st Jan 2020, 4:38 PM
Ankur Singh Oli
Ankur Singh Oli - avatar
6 odpowiedzi
+ 8
Your code takes too long for N>30, so it timeouts and prints an error. Try using memoization to speed up your code. https://www.geeksforgeeks.org/memoization-1d-2d-and-3d/
1st Jan 2020, 4:48 PM
Diego
Diego - avatar
+ 7
How much maximum value of 'n' you think for which your code can calculate Q(n) ? Q(n) / \ Q(n-1) Q(n-2) / \ / \ Q(n-2) Q(n-3) Q(n-3) Q(n-4) . . . . . . . . Seems like too much function calls there, which is making it slow and giving TLE(time limit exceeded). Possible solution of it can be using basic dynamic programming, you can see that you are calculating Q(n-2) & Q(n-3) multiple times(In above structure I made) so if you can store it somewhere so you not calculate it multiple times and avoid unnecessary space and time, thats how you can make your program more faster & efficient.
1st Jan 2020, 4:46 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
That is a pro challenge. I sent you a private message, because the solutions to the challenges should not be posted. I'll rewrite it here. You can send me a resume of the challenge along with your solution, and I'll try to help.
1st Jan 2020, 4:45 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
Thanks everyone.
1st Jan 2020, 5:01 PM
Ankur Singh Oli
Ankur Singh Oli - avatar
+ 2
Ankur Singh Oli 🐍 Memoization: Qn = {1:1, 2:1}
3rd Jan 2020, 11:10 AM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
0
what about test case 6.. I managed to get all tests correct except for that one?!
4th Dec 2020, 11:24 AM
Mostafa Ehab
Mostafa Ehab - avatar