Continue learning
Practice related DSA questions in the same topic.
Compute target factorial by recording pending factors, then unwinding from the base case. The values list is unused.
Compute target factorial by recording pending factors, then unwinding from the base case. The values list is unused.
def solve(values, target): stack = [0] * (target) top = 0 n = target while (n > 0): stack[top] = n top = (top + 1) n = (n - 1) result = 1 while (top > 0): top = (top - 1) result = (result * stack[top]) return resultEmpty structure
Push a pending factorial argument.
Push a pending factorial argument.
Leave one signed order slip at every desk, then collect the answers in reverse desk order.
Replace each factorial call with a pending multiplication. Begin returning at zero factorial, whose value is one.
O(t) time · O(t) auxiliary space
Worked example · {"values":[],"target":5} ⇒ 120
Edge case 1 · {"values":[],"target":0} ⇒ 1
Edge case 2 · {"values":[],"target":1} ⇒ 1
An empty product equals one, including zero factorial.
How does a direct recursive function store the same pending work?