CodeISM Class 18 (Introduction To DP)
CodeISM Class 18 (Introduction To DP)
Let us take x = 4
Number of function calls = 1 + 2 + 4 + 8 + 16
= 31
= 25 -1
For if call f(n) , you will get 2(n+1) - 1 function calls
So, the time complexity = O( 2*2n - 1) = O(2n)
int f(int x)
{
if(x==0)
{
return 2;
}
else
{
return f(x-1) * 2;
}
}
You can try this website for visualising the recursion tree:
https://recursion.now.sh/
Intuition of Dynamic Programming
1+2+6+7+5 =?
21
1+2+6+7+5 +2=?
21 + 2 = 23
Fibonacci Numbers
N : 1, 2, 3, 4, 5, 6…...
F(N) : 0, 1, 1, 2, 3, 5….
Recurrence relation:
F(N) = F(N-1) + F(N-2)
#include <bits/stdc++.h>
int fib(int n)
{
if(n==1)
return 0;
if(n==2)
return 1;
if(dp[n] != -1)
{
return dp[n];
}
return dp[n]=fib(n-1) + fib(n-2); // Memoization
}
int main() {
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
dp[1]=0;
dp[2]=1;
cout<<fib(n);
return 0;
}
Wherever we see a recursive solution that has repeated calls for the
same inputs, we can optimize it using Dynamic Programming. The
idea is to simply store the results of subproblems, so that we do
not have to re-compute them when needed later. This simple
optimization reduces time complexities from exponential to
polynomial.
Bottom Up Approach:
Analogy to understand:
I am going to learn to program. Then, I will start practising. Then, I will
participate in coding contests. I will improve by solving those
questions which I couldn't solve during every contest. I will be able to
crack an internship at a good company.
In Bottom-up you start with the small solutions (base case) and build
up.
Advantages :
1. Fast and uses less memory than top down.
2. Shorter Code
Analogy to understand:
I will be able to crack an internship at a good company. How? I will
improve by solving those questions which I couldn't solve during every
contest. How? I will participate in coding contests. How? I will start
practicing? I am going to learn to program.
Advantages :
1. Easy to apply
2. Order doesn't matter.
Q: https://atcoder.jp/contests/dp/tasks/dp_a
Recursion Solution:-
#include<bits/stdc++.h>
using namespace std;
vector<int> h;
vector<int> Memo;
int climbStairs(int n) {
vector<int> dp(n+1);
//dp[i]-> number of ways to reach at i-th floor
dp[0]=1;
dp[1]=1;
for(int i=2;i<=n;i++) dp[i]=dp[i-1]+dp[i-2];
return dp[n];
}
● H.W- Solve the last problem using Recursive DP
Practice Problems:
1. https://atcoder.jp/contests/dp/tasks/dp_b
2.
https://www.hackerearth.com/practice/algorithms/dynamic-programmin
g/introduction-to-dynamic-programming-1/practice-problems/algorithm
/jump-k-forward-250d464b/
3. https://atcoder.jp/contests/dp/tasks/dp_c
4. https://atcoder.jp/contests/abc129/tasks/abc129_c
5. https://codeforces.com/problemset/problem/1245/C
6. https://codeforces.com/problemset/problem/455/A%7C
7. https://codeforces.com/problemset/problem/1195/C
8. https://www.spoj.com/problems/ACODE/
9. https://codeforces.com/problemset/problem/189/A