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?

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.

Further reading

Problem with a question

Algorithms and Data Structures · Senior · question 53

Test yourself

Take the Algorithms and Data Structures · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz