Algorithms and Data Structures · Senior · Question 53 of 75
A recursive solution is correct but times out because of repeated subproblems with three parameters. What's the usual first fix?
- ✓Correct answer: Memoize each parameter tuple, e.g. in a
Map - 2Rewrite it in a faster compiled language
- 3Increase the recursion limit of the JS engine
Explanation
The number of distinct states bounds the work once each state is cached. In JavaScript a key such as ${i},${j},${k} or a 3D array works. Then consider bottom-up order to avoid deep recursion.