Optimize a recursive algorithm
Convert a slow recursive function to an iterative or memoised solution, or apply dynamic programming when applicable. Benchmarks original vs optimised.
Preview
Copy prompt
The following recursive function is too slow for inputs larger than [N]: [Paste code]. Analyse its time and space complexity. Convert it to an iterative implementation using an explicit stack to eliminate call overhead. If overlapping subproblems exist, add memoization. If optimal substructure exists, show a bottom-up dynamic programming solution. Benchmark all versions with inputs of increasing size to show where each approach excels.
More
Performance
Prompts
