

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 6
click for more info
Not enough gems
Cost: 6 gems
1: Polynomial vs. Exponential
incomplete
2: Polynomial Time = P
incomplete
3: Reduction to P
incomplete
4: Order K^N – Exponential
incomplete
5: Big O Categories Review
incomplete
6: Complexity Quiz - Example 1
incomplete
7: Complexity Quiz - Example 2
incomplete
8: Complexity Quiz - Example 3
incomplete
9: Exponential Growth Sequences
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Let's take an exponential time algorithm and fix it up so it can run in polynomial time!
The Fibonacci sequence is a sequence of numbers where each number is the sum of the two numbers before it. Like this:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
We want a function that, given an index in the sequence, returns the Fibonacci number at that index. For example:
fib(0) -> 0fib(1) -> 1fib(2) -> 1fib(3) -> 2fib(4) -> 3fib(5) -> 5fib(6) -> 8fib(7) -> 13Here are the implementation details to do it in polynomial time:
n represents the index of the desired Fibonacci number.n is less than or equal to 1, then return n.grandparent = 0, parent = 1, and a placeholder current to store the new Fibonacci number at each step.n - 1 times. (For example, if n = 2, one iteration occurs.)current = parent + grandparentparent and grandparent) to maintain the sequence.current.Our data scientists at LockedIn have found that the growth of the average influencer's follow count is roughly the same growth rate as the Fibonacci sequence! In other words, after 6 weeks of good social media posts, the average influencer will have 8 followers. After 7 weeks, 13 followers, and so on.
The trouble is, our current implementation of the fib function takes so long (exponential time!) to complete that when our influencers navigate to their analytics page it often never completes loading!
Adjust the fib function using the given algorithm to achieve polynomial runtime.