Skip to content

Commit 40c7c49

Browse files
committed
added more dp problems
1 parent e9bfad8 commit 40c7c49

9 files changed

+299
-19
lines changed

Maximum_Dot_Product_Subsequence.cpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ int maxDotProduct(vector<int>& A, vector<int>& B,int size1,int size2) {
1515
return dp[size1-1][size2-1];
1616
}
1717

18+
1819
int main(){
1920
int size1,size2;
2021
cin>>size1>>size2;
@@ -27,4 +28,22 @@ int main(){
2728
cin>>B[i];
2829
}
2930
cout<<maxDotProduct(A,B,size1,size2)<<"\n";
30-
}
31+
}
32+
33+
// another approach
34+
class Solution {
35+
public:
36+
int maxDotProduct(vector<int>& nums1, vector<int>& nums2) {
37+
int n = int(nums1.size()), m = int(nums2.size());
38+
vector<vector<int>> dp(n + 1, vector<int>(m + 1, INT_MIN));
39+
for (int i = 1; i <= n; ++i) {
40+
for (int j = 1; j <= m; ++j) {
41+
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
42+
dp[i][j] = max(dp[i][j], dp[i][j - 1]);
43+
dp[i][j] = max(dp[i][j], dp[i - 1][j - 1]);
44+
dp[i][j] = max(dp[i][j], max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1]);
45+
}
46+
}
47+
return dp[n][m];
48+
}
49+
};

README.md

+26-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Dynamic-Programming
22
A DP a day keeps the bug away.
33

4-
**Cointains Basic Dp Problems**<br><br>
4+
**Cointains Dp Problems**<br><br>
55

66
**Parent Problem** : [*Kadane's Algorithm*](/Kadane.cpp)<br>
77
1) [Best Time to Buy and sell Stock(Leetcode)](/Best_Time_To_Buy_And_Sell_Stock_.cpp)<br>
@@ -59,14 +59,24 @@ SubProblems :<br>
5959
5) [Maximum Product Increasing Subsequence](/Maximum_Product_Increasing_Subsequence.cpp).<br>
6060
6) [Minimum Jumps to reach end](/Minimum_Jumps_To_Reach_End.cpp).<br>
6161
7) [Box Stacking Problem](/Box_Stacking_Problem.cpp).<br>
62-
8) [Longest Bitonic Sequence](/BitonicSequence.cpp).<br><br>
62+
8) [Longest Bitonic Sequence](/BitonicSequence.cpp).<br>
63+
9) [Longest Alternating Subsequence](/longest_alternating_subsequence.cpp)<br>
64+
10) [Count All Increasing Subsequences](/Count_All_Increasing_Subsequences.cpp).<br>
6365

6466
**Parent Problem** : *DP + Greedy*<br>
6567
1) [Jump Game-2(Leetcode)](/jump_game_2..cpp).<br>
6668
2) [Video Stiching(Leetcode)](/video_stiching.cpp).<br>
67-
3) [Minm number of taps to open water](/minm_number_of_taps_to_open_water.cpp)
68-
69-
**Parent Problem** : [*Count Distinct SubSequences*](/Count_Distinct_Subsequences.cpp)<br>
69+
3) [Minm number of taps to open water](/minm_number_of_taps_to_open_water.cpp).<br>
70+
71+
**Parent Problem** : *Game(DP) + minimax*<br>
72+
1) [Nim Game](/nim_game.cpp).<br>
73+
2) [Flip Game-1](/flip_game.cpp).<br>
74+
3) [Flip Game-2](/flip_game2.cpp).<br>
75+
4) [Get Minimum Squares](/Get_Minimum_Squares.cpp).<br>
76+
5) [Optimal Strategy to play a Game](/Optimal_Strategy_To_Play_A_Game.cpp).<br>
77+
6) [Stone Game-3](/Last_Stone_3.cpp)<br>
78+
7) [Jump Game-3(Leetcode)](/Jump_Game_3.cpp)<br>
79+
8) [Jump Game-5(Leetcode)](/Jump_Game_5.cpp)<br>
7080

7181
**Parent Problem** : *Dp on Grids*<br>
7282
1) [Maximum Size Submatrix Square](/Maximum_Size_Submatrix_Square.cpp).<br>
@@ -76,28 +86,26 @@ SubProblems :<br>
7686
5) [Path in a Matrix](/Max_path_sum.cpp)<br>
7787
6) [Maximal Square(Leetcode)](/maximal_square.cpp)<br>
7888
7) [Count Submatrices with all ones(Leetcode)](/count_submatrices_with_all_ones.cpp)<br>
79-
8) [Maximum Sum Submatrix](/maximum_sum_submatrix.cpp)
89+
8) [Maximum Sum Submatrix](/maximum_sum_submatrix.cpp)<br>
90+
9) [Unique Path-1](/unique_paths_1.cpp)<br>
91+
10) [Unique Path-2](/unique_paths_2.cpp)<br>
92+
8093

8194
**Parent Problem** : [*Matrix Chain Multiplication*](/Matrix_Chain_Multiplication.cpp)<br>
8295

96+
**Parent Problem** : [*Count Distinct SubSequences*](/Count_Distinct_Subsequences.cpp)<br>
97+
8398
**Parent Problem** : *Dp on Trees*<br>
8499
1) [Max Path Sum](/Max_path_sum.cpp)<br>
85100
2) [Maximum Path Sum in a Tree from Any node to any other node](/Maximum_Path_Sum_in_Binary_tree.cpp)<br>
86101
3) [Diameter of a Binary Tree](/Diameter_Of_Binary_Tree.cpp)<br>
87102

88103
**Others**
89-
1) [Get Minimum Squares](/Get_Minimum_Squares.cpp).<br>
90-
2) [Count All Increasing Subsequences](/Count_All_Increasing_Subsequences.cpp).<br>
91-
3) [Longest Consequetive Subsequences](/Longest_Consequetive_Subsequence.cpp).<br>
92-
4) [Optimal Strategy to play a Game](/Optimal_Strategy_To_Play_A_Game.cpp).<br>
93-
5) [Zeroes And Ones](/zerosAndOnes.cpp)
94-
6) [Longest Pallidromic SubString](/Longest_Pallindromic_Substring.cpp).<br>
95-
7) [Count Pallindromic substring of string](/Count_Pallindromic_SubString_Of_String_gfg.cpp)<br>
96-
8) [Product of array except self](/Count_Pallindromic_SubString_Of_String_gfg.cpp)
97-
9) [Jump Game-3(Leetcode)](/Jump_Game_3.cpp)<br>
98-
10) [Jump Game-5(Leetcode)](/Jump_Game_5.cpp)<br>
99-
11) [Stone Game-3](/Last_Stone_3.cpp)<br>
100-
12) [Num Products](#)<br>
104+
1) [Longest Consequetive Subsequences](/Longest_Consequetive_Subsequence.cpp).<br>
105+
2) [Zeroes And Ones](/zerosAndOnes.cpp)
106+
3) [Longest Pallidromic SubString](/Longest_Pallindromic_Substring.cpp).<br>
107+
4) [Count Pallindromic substring of string](/Count_Pallindromic_SubString_Of_String_gfg.cpp)<br>
108+
5) [Product of array except self](/Count_Pallindromic_SubString_Of_String_gfg.cpp)
101109

102110
Happy Coding!!
103111

count_subsets_of_given_difference.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// given an array a difference of subset.find the count of that difference
5+
// approach ----------------> s1 + s2 = sum(arr) --eqn1 && s1-s2 = diff(given) ---eqn2;
6+
// solve eqn1 & eqn2 find s1 . s1 = (sum+diff)/2
7+
// therefore the problem reduces to count number of subset of with sum s1
8+
9+
int count(vector<int> &nums,int sum) {
10+
int n = nums.size();
11+
vector<vector<int>>dp(n+1,vector<int>(sum+1,0));
12+
for(int i=0;i<=sum;i++){
13+
dp[0][i] = 0;
14+
}
15+
for(int i=0;i<=n;i++){
16+
dp[i][0] = 1;
17+
}
18+
for(int i=1;i<=n;i++){
19+
for(int j=1;j<=sum;j++){
20+
if(nums[i-1] <= j){
21+
dp[i][j] = dp[i-1][j] + dp[i-1][j-nums[i-1]];
22+
}
23+
else{
24+
dp[i][j] = dp[i-1][j];
25+
}
26+
}
27+
}
28+
return dp[n][sum];
29+
}
30+
31+
int main() {
32+
int n,diff,sum=0;
33+
cin>>n>>diff;
34+
vector<int>nums(n);
35+
for(int i=0;i<n;i++){
36+
cin>>nums[i];
37+
sum += nums[i];
38+
}
39+
int s1 = (sum+diff)/2;
40+
cout<<count(nums,s1);
41+
}

flip_game_1.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// problem : https://www.lintcode.com/problem/flip-game/
5+
6+
class Solution {
7+
public:
8+
vector<string> generatePossibleNextMoves(string &s) {
9+
// write your code here
10+
int n=s.length();
11+
vector<string>ans;
12+
for(int i=0;i<n-1;i++){
13+
if(s[i] == '+' && s[i+1] == '+'){
14+
s[i] = s[i+1] = '-';
15+
ans.push_back(s);
16+
s[i] = s[i+1] = '+';
17+
}
18+
}
19+
return ans;
20+
}
21+
};

flip_game_2.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// problem : https://www.lintcode.com/problem/flip-game-ii/
5+
// very good question
6+
7+
class Solution {
8+
public:
9+
bool canWin(string &s) {
10+
// write your code here
11+
if(s.length()<2) return false;
12+
return flipGame(s);
13+
}
14+
15+
bool flipGame(string &s){
16+
for(int i=0;i<s.length()-1;i++){
17+
if(s[i]=='+' && s[i+1]=='+'){
18+
// find the next state of the string and see if the other player can win
19+
// with the next state
20+
string nextState = s.substr(0,i)+"--"+s.substr(i+2);
21+
// if the player can't win with the next state, i will win the flipGame
22+
// and hence return true
23+
if(!flipGame(nextState)){
24+
return true;
25+
}
26+
}
27+
}
28+
return false;
29+
}
30+
};

longest_alternating_subsequence.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// problem : https://leetcode.com/problems/wiggle-subsequence
5+
6+
// O(n^2) time and o(n) space
7+
class Solution {
8+
public:
9+
int wiggleMaxLength(vector<int>& nums) {
10+
int n=nums.size();
11+
if(n == 0) return 0;
12+
// up[i] refers to the length of the longest wiggle subsequence obtained so far considering ith element
13+
vector<int>up(n,1),down(n,1);
14+
for(int i=1;i<n;i++){
15+
for(int j=0;j<i;j++){
16+
if(nums[i] > nums[j]){
17+
up[i] = max(up[i],1+down[j]);
18+
}
19+
else if(nums[i] < nums[j]){
20+
down[i] = max(down[i],1+up[j]);
21+
}
22+
}
23+
}
24+
return max(up[n-1],down[n-1]);
25+
}
26+
};
27+
28+
29+
// O(n) time and O(n) space
30+
31+
class Solution {
32+
public:
33+
int wiggleMaxLength(vector<int>& nums) {
34+
int n=nums.size();
35+
if(n == 0) return 0;
36+
// up[i] refers to the length of the longest wiggle subsequence obtained so far considering ith element
37+
vector<int>up(n,1),down(n,1);
38+
for(int i=1;i<n;i++){
39+
if(nums[i] > nums[i-1]){
40+
up[i] = 1+down[i-1];
41+
down[i] = down[i-1];
42+
}
43+
else if(nums[i] < nums[i-1]){
44+
down[i] = 1+up[i-1];
45+
up[i] = up[i-1];
46+
}
47+
else{
48+
down[i] = down[i-1];
49+
up[i] = up[i-1];
50+
}
51+
}
52+
return max(up[n-1],down[n-1]);
53+
}
54+
};
55+
56+
57+
58+
// O(n) ime and O(1) space
59+
class Solution {
60+
public:
61+
int wiggleMaxLength(vector<int>& nums) {
62+
int n=nums.size();
63+
if(n == 0) return 0;
64+
int up=1,down=1;
65+
for(int i=1;i<n;i++){
66+
if(nums[i] > nums[i-1]){
67+
up = down+1;
68+
}
69+
else if(nums[i] < nums[i-1]){
70+
down = up+1;
71+
}
72+
}
73+
return max(up,down);
74+
}
75+
};

nim_game.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// problem : https://leetcode.com/problems/nim-game
5+
6+
// n > 10^9 won't work, O(n) solution
7+
class Solution {
8+
public:
9+
bool canWinNim(int n) {
10+
if(n <= 3) return true;
11+
bool dp[n+1];
12+
dp[0]=false;
13+
dp[1]=true,dp[2]=true,dp[3]=true;
14+
for(int i=4;i<=n;i++){
15+
dp[i] = !(dp[i-1] && dp[i-2] && dp[i-3]);
16+
}
17+
return dp[n];
18+
}
19+
};
20+
21+
// O(1) solution
22+
23+
class Solution {
24+
public:
25+
bool canWinNim(int n) {
26+
return !(n%4 == 0);
27+
}
28+
};

unique_paths_1.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// problem: https://leetcode.com/problems/unique-paths/
5+
6+
class Solution {
7+
public:
8+
int uniquePaths(int m, int n) {
9+
vector<vector<int>>dp(m,vector<int>(n,1));
10+
for(int i=1;i<m;i++){
11+
for(int j=1;j<n;j++){
12+
dp[i][j]=dp[i-1][j]+dp[i][j-1];
13+
}
14+
}
15+
return dp[m-1][n-1];
16+
}
17+
};

unique_paths_2.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// problem : https://leetcode.com/problems/unique-paths-ii
5+
6+
class Solution {
7+
public:
8+
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
9+
int n=obstacleGrid.size(),m=obstacleGrid[0].size();
10+
vector<vector<int>>dp(n,vector<int>(m,1));
11+
int f=0;
12+
for(int i=0;i<n;i++){
13+
if(obstacleGrid[i][0] == 1){
14+
f = 1;
15+
}
16+
if(f == 1){
17+
dp[i][0] = 0;
18+
}
19+
}
20+
f=0;
21+
for(int i=0;i<m;i++){
22+
if(obstacleGrid[0][i] == 1){
23+
f = 1;
24+
}
25+
if(f == 1){
26+
dp[0][i] = 0;
27+
}
28+
}
29+
for(int i=1;i<n;i++){
30+
for(int j=1;j<m;j++){
31+
if(obstacleGrid[i][j] == 0){
32+
dp[i][j] = dp[i-1][j] + dp[i][j-1];
33+
}
34+
else{
35+
dp[i][j] = 0;
36+
}
37+
}
38+
}
39+
return dp[n-1][m-1];
40+
}
41+
};

0 commit comments

Comments
 (0)