Count the calls of fib
Standard · Write code · about 8 min
Return how many calls the naive fib(n) makes in total, the first call included. fib(0) and fib(1) make 1 call each.
Solve it with a recurrence on the counts, not by running fib.
Example 1
Input:
[5]
Output:
15
fib(3) alone makes 5 calls; fib(5) makes 15.
Example 2
Input:
[4]
Output:
9
Constraints
- 0 ≤ n ≤ 25
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
- Repeated subproblems Standard
- Climbing stairs Standard
- Count leaves of the call tree Challenge