advanced
Recursive Optimization with Memoization
Optimize a highly inefficient recursive algorithm and analyze the time-space trade-off.
📝 Promptens innehåll
Given the following naive recursive function for calculating the nth term of a custom tribonacci sequence with distinct modulo operations: [Function Pseudocode]. Analyze its time complexity and identify the redundant calculations. Refactor the code to implement memoization (top-down dynamic programming). Then, convert the solution into an iterative approach (bottom-up) with O(1) space complexity. Provide a mathematical proof of correctness for the iterative version and compare the stack memory usage between the three versions.