Repeated subproblems
Standard · Trace the algorithm · about 5 min
The tree of fib(5) is complete. Some calls recompute a fib(2) or fib(3) that an earlier call already worked out. Click every such repeated call.
Example 1
Input:
{"n":5}
Output:
5
Constraints
- Count only fib(2) and fib(3) calls that are not the first with their n.
Learn the idea first: Recursion trees: Fibonacci and repeated work, a visual lesson.
More Recursion trees: Fibonacci and repeated work problems
- Calls in fib(5) Warm-up
- Fibonacci, the naive way Warm-up
- Order of calls Standard
- Count the calls of fib Standard
- Climbing stairs Standard
- Count leaves of the call tree Challenge