0% found this document useful (0 votes)
19 views

Dynammic Programming

The document discusses several dynamic programming problems and their solutions. It provides approaches and equations for problems involving counting palindromic subsequences in a string, optimal stock buying and selling, longest common subsequence, and matrix chain multiplication. Code snippets and pastebin links are included for bottom-up and optimal solutions.

Uploaded by

krishna soni
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Dynammic Programming

The document discusses several dynamic programming problems and their solutions. It provides approaches and equations for problems involving counting palindromic subsequences in a string, optimal stock buying and selling, longest common subsequence, and matrix chain multiplication. Code snippets and pastebin links are included for bottom-up and optimal solutions.

Uploaded by

krishna soni
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Placement and

Internship Round-4
Discussion
 Approach
The states of dp will be
1. N
2. Number of times the number 4 occurred

For each N we will ask the number of ways it can be formed


using N-1, N-2, N-4 and N-6

F(N, 2) = F(N - 1, 2) + F(N – 2, 2) + F(N – 4, 1) + F(N – 6, 2)


F(N, 1) = F(N - 1, 1) + F(N – 2, 1) + F(N – 4, 0) + F(N – 6, 1)
F(N, 0) = F(N - 1, 0) + F(N – 2, 0) + F(N – 6, 0)
 Top-down Code: https://pastebin.com/QY2Rmv3A
 Bottom Up code: https://pastebin.com/sps1SEUT
 Approach
The dp states will be
1.index
2.number of items.

Firstly, there is no sense to items being greater than N, as this will never give the
optimal answer. If k > N, then we are buying more items than necessary.
Since we have only 3 options, whether to buy, sell or do nothing,
So, our equation will be
(do nothing) (buy the item on that day)
F(index, items) = F(index+1, items) + F(index + 1, items + 1) – prices[index]
+ F(index + 1, items - 1) + prices[index]
(sell the item on that day)
 Solution : https://pastebin.com/Q89wCn5g
 Approach
This is a Knapsack question!
We have to make 2 disjoint subsequences. We will try to make both simultaneously
since we only need to find the maximum length.
So, for each character of the string, we are asking whether to
1. Send it to non-decreasing subsequence
2. Send it to non-increasing subsequence
3. Do nothing with it

So, our dp states will be


4. Index
5. Last character taken of non-decreasing subsequence
6. Last character taken of non-increasing subsequence
 Solution: https://pastebin.com/xGsxMy8S
 Approach:
TLE approach: The standard way of doing it is making 3 states, i, j and k
i and j represent that we have considered string from 0 to i and from j to n-1, and
calculated the number of subsequences of length k (remember that k is a state
here).

So our dp equation will be


F(i, j, k) = F(i+1, j, k) + F(i, j-1, k) - F(i+1, j-1, k)
If(s[i] == s[j]) then F(i, j, k) += F(i+1, j-1, k-2)

Time Complexity: O(N*N*K)


 Optimal Approach:
Since K is 10 only, and our string is a binary string!
So, we can take the advantage of that.
We can construct all palindromic strings of length K check its number of
occurrences in the given string.
Since it’s a binary string, so we either set a character 0 or 1.
What we can do is make a string of size K/2 and concatenate its reverse.
This will take 2^(K/2) time for construction.
Then we will check its occurrences count using dp.

Equation will be
F(i, j) = F(i+1, j) + F(i+1, j+1) if s[i] = t[j], where s is our given string and t is the
constructed palindromic string of length K
 Approach
It’s a knapsack variation.
The states of dp will be
1. Index (i)
2. Count (j)
dp[i][j] = no of ways to do exactly j removals after visiting starting i
elements.
Base Condition: dp[0][0] = 1.
dp[i][j] = dp[i – 1][j] (not picking)
If we can remove the segment [i – m + 1........ i] then only we can
increase the count [picking condition].
i.e. dp[i][j] = dp[i – m][j - 1] (picking)

Time Complexity O(n^2 * m) (TLE ?)


 Some more optimisations?
 We can create a boolean vector to check if we remove a
string from current index (i).
 i.e. a[i] = true if we can remove the segment [i – m +1 …. i].

 so now our dp transition will actually look like follows.


 dp[i][j] += dp[i – 1][j] (not picking)
 dp[i][j] += dp[i – m][j – 1] (picking) if a[i] is true.

 Now the TC will be O(n^2)


 Bottom-Up Code: https://p.ip.fi/ah53
#include <bits/stdc++.h> vector<vector<int>> dp(n + 1, vector<int>(n + 2));

using namespace std; dp[0][0] = 1;

const int mod = 1e9 + 7; for (int i = 1; i <= n; i++){

void solve(){ for (int j = 0; j <= n + 1; j++){

int n, m; cin >> n >> m; dp[i][j] += dp[i - 1][j];

string s, t; cin >> s >> t; dp[i][j] %= mod;

vector<bool> a(n, true); if (a[i - 1]){

for (int i = 0; i < n; i++){ dp[i][j + 1] += dp[i - m][j];

for (int j = 0; j < m; j++){ dp[i][j + 1] %= mod;


}
if (i - j < 0 or s[i - j] != t[m - 1 - j]){
}
a[i] = false;
}
break;
for (int i = n; i >= 0; i--) if (dp[n][i] > 0){
}
cout << i << " " << dp[n][i] << endl;
}
}
}
}
 Approach:
This is a very tricky mcm dp question.
If we do in our standard mcm way, we will take states i and j, then traverse k from
i+1 to j and consider F(i, k) and F(k+1, j).

Eg. acbbbcdca -> a + cbbbcdc + a

score[2] + c + bbbcd + c

score[2] + c + bbb + cd + c

score[2] + score[3] + ccc + d

score[2] + score[3] + score[3] + score[1]


BUT THERE IS A BIG PROBLEM HERE!

Our recurrence relations are dependent of each other!

So we will take another state count, which represents the length we have considered till
now.
Now our equation will be:

for(int k = i+1; k<=j; k++)


{
if(s[k] == s[i])
{
F(I, j, count) = max(F(i+1, k-1, 0) + F(k, j, count + 1))
}
}

Base case: when i == j, return score[count]


 Solution:

You might also like