Dynamic Programming Tutorial with Fibonacci Sequence
Skills:
Algorithm Basics90%
Key Takeaways
Calculates the Fibonacci Sequence using Dynamic Programming
Full Transcript
hey guys this is my introduction to dynamic programming with Fibonacci Sequence as our example now what's fibon sequence it's a sequence of numbers that start with ones at the beginning two ones at the beginning and after that we just add the previous two numbers to get the next number so 1 + 1 = 2 and 1 + 2 = [Music] 3 and 2 + 3 = 5 and so on and the problem here is finding the nth Fibonacci number so if n is six the six Fibonacci number is just eight the most naive approach we can think of for this problem is a recursive one so in this code I'm defining my function here fib and if n is equal to one or two we just return one which is base case and that corresponds to the first two elements and if that's not the case we're just going to return the sum of the previous element and the second previous element so this approach works but the thing is this is very very inefficient let's see why that's the case here's a quick analysis of the naive recursive approach that we just talked about let's just say as an example we are trying to find the six fibon number to do that in this approach what we do is we ask ourselves what's the fifth F number number and what's the fourth FIB not number and when we know what they are we add them together to find the sixth fibon Notch number and to find the fifth Fage number we need to find the fourth fan number the third Fage number and then add them together that's what this diagram shows and as you can see the problem with this approach is that there are a lot of duplicates in our computation for instance to find the third fiban number we're repeating the same process three times and to find the fourth fibon number we're repeating the same competion twice and that's why this is very slow and in fact the time it takes to find the n f number is about an order of 2 to the^ of N and dynamic programming says we can do much better than that just by storing all those intermediate results so here's one very simple dynamic programming solution in this example we're going to assume that n is larger than two just for simplic s and let's just say we're trying to find the six fibach number as our previous example and the first thing we do is we initialize an array of the same size as the given number and we are initializing it to zeros here but it doesn't matter if we initialize it to ones or negative ones and obviously the first two elements need to be one and we're going to iterate this I from two which corresponds to the third element here to n minus one which corresponds to the last element here and for each I we're going to populate it with the sum of the previous number and the second previous number so for the third element we get 1 + 1 = 2 and for the fourth element we get 1+ 2 = 3 and five and so on and at the end we're just going to return the last element in the array which is eight in this case this is a much better solution than the previous solution to find N's fibon number it only takes a linear time so it's much faster but you might say oh but we are using all this extra space and if you don't like that that's fine you can just store the last two elements instead of of everything and you can save some space that way as well that's it for my video If you like this video you might also like my video about the maximum subarray problem for which you can also use a dynamic uh programming solution and if you want to watch more videos like this one you can just click right here to subscribe to my channel and see you soon
Original Description
Here's a quick dynamic programming tutorial with Fibonacci Sequence!
Keywords:
dynamic programming fibonacci sequence
dynamic programming fibonacci numbers
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CS Dojo · CS Dojo · 2 of 60
1
▶
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
Longest Common Subsequence (Dynamic Programming)
CS Dojo
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
Coding Interview Question: Tower Hopper Problem
CS Dojo
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
How I Got an Internship at Microsoft
CS Dojo
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
Why I Left My $100,000+ Job at Google
CS Dojo
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
How I Learned to Code - and Got a Job at Google!
CS Dojo
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
What Is Dynamic Programming and How To Use It
CS Dojo
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
What Programming Language Should I Learn First?
CS Dojo
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
Using Boolean in Python (Python Tutorial #11)
CS Dojo
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
What Can You Do with Python? - The 3 Main Applications
CS Dojo
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
Google Coding Interview - Universal Value Tree Problem
CS Dojo
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
AI Tools that Actually Pay You Back: A Developer's Guide to Monetizing AI
Dev.to AI
The new ChatGPT macOS app redesign has made basic navigation so much worse
Reddit r/artificial
Three Token-2022 Mints in One Week: Fees, Yield, and Soulbound
Dev.to · atharv shukla
Maximize Google Workspace AI Power: Safeguard Data and Boost Performance in 2026
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI