Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Dynamic Programming/01KnapsackDP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 0_1_Knapsack
// Input :
// 4
// 7
// 1 4 5 7
// 1 3 4 5
// Output :
// 9

#include<bits/stdc++.h>
#include<cmath>
using namespace std;
int KnapsackDp(int fw, int values[], int weight[], int n)
{
int dp[n+1][fw+1];
for(int i=0; i<=n; i++)
dp[i][0] = 0;
for(int i=0; i<=fw; i++)
dp[0][i] = 0;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=fw; j++)
{
if(weight[i-1] > j)
{
dp[i][j] = dp[i-1][j];
}
else{
dp[i][j] = max(values[i-1] + dp[i-1][j-weight[i-1]], dp[i-1][j]);
}
}
}
return dp[n][fw];
}
int main()
{
int n, fw;
cin>>n;
cin>>fw;
int values[n], weight[n];
for(int i=0; i<n; i++)
cin>>values[i];
for(int i=0; i<n; i++)
cin>>weight[i];
cout<<KnapsackDp(fw, values, weight, n);
return 0;
}
28 changes: 28 additions & 0 deletions Dynamic Programming/ClimbingStairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Climbing Stairs Problem
// Input : 5
// Output : 8

#include <bits/stdc++.h>
using namespace std;

int climbStairs(int n)
{
int dp[n + 2];
dp[0] = 0;
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++)
{
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}

int main()
{
int n;
//Input number of stairs
cin >> n;
int ans = climbStairs(n);
cout << ans << endl;
}
48 changes: 48 additions & 0 deletions Dynamic Programming/CoinChangingProblem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Coin Change Problem
// Input :
// 3
// 5
// 1 2 3
// Output :
// 2

#include <bits/stdc++.h>
using namespace std;

int coinchange(vector<int>& a, int sum, int n,
vector<vector<int> >& dp)
{
if (sum == 0)
return dp[n][sum] = 1;
if (n == 0)
return 0;
if (dp[n][sum] != -1)
return dp[n][sum];
if (a[n - 1] <= sum) {
// Either Pick this coin or not
return dp[n][sum] = coinchange(a, sum - a[n - 1], n, dp)
+ coinchange(a, sum, n - 1, dp);
}
else // We have no option but to leave this coin
return dp[n][sum] = coinchange(a, sum, n - 1, dp);
}

int32_t main()
{
int tc = 1;
//cin >> tc;
while (tc--) {
int n, sum;
// Input the no of coins
cin >> n;
// Input the sum to get
cin >> sum;
// Input Coins denominations
vector<int> a(n);
for(int i=0; i<n; i++) cin >> a[i];
vector<vector<int> > dp(n + 1,
vector<int>(sum + 1, -1));
int res = coinchange(a, sum, n, dp);
cout << res << endl;
}
}
59 changes: 59 additions & 0 deletions Dynamic Programming/Count_No_Of_Subsets_with_given_diff.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// No of Subsets with given diff
// Input :
// 4
// 1 1 2 3
// 1
// Output :
// 3

#include <iostream>
using namespace std;
//TARGET SUM IS SAME AS THIS
class starry
{
public:
int count (int arr[],int sum,int n)
{
int t[n][sum+1];
for(int i=0;i<n+1;i++)
{
for(int j=0;j<sum+1;j++)
{
if(i==0)
t[i][j]=0;
if(j==0)
t[i][j]=1;
}
}
for(int i=1;i<n+1;i++)
{
for(int j=1;j<sum+1;j++)
{
if(arr[i-1]<=j)
t[i][j]=t[i-1][j-arr[i-1]]+t[i-1][j];
else
t[i][j]=t[i-1][j];
}
}
return t[n][sum];


}
};
int main()
{
starry ob;
// Input the array
int n; cin >> n;
int arr[n];
for(int i=0; i<n; i++) cin >> arr[i];
// Input diff you want to get
int diff; cin >> diff;
int total_sum=0,sum;
for(int i=0;i<n;i++)
{
total_sum+=arr[i];
}
sum=(diff+total_sum)/2;
cout<<ob.count(arr,sum,n);
}
47 changes: 47 additions & 0 deletions Dynamic Programming/Count_Of_subsets_with_given_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Count of subsets with the given sum
// Input :
// 6
// 2 3 5 6 8 10
// 10
// Output :
// 3

#include<iostream>
using namespace std;
int subset_sum(int arr[],int sum,int n)
{
int t[n+1][sum+1];
for(int i=0;i<n+1;i++)
{
for(int j=0;j<sum+1;j++)
{
if(i==0)
t[i][j]=0;
if(j==0)
t[i][j]=1;
}
}
for(int i=1;i<n+1;i++)
{
for(int j=1;j<sum+1;j++)
{
if(arr[i-1]<=j)
t[i][j]=t[i-1][j-arr[i-1]]+t[i-1][j];
else
t[i][j]=t[i-1][j];
}
}
return t[n][sum];
}
int main()
{
// Input the array
int n; cin >> n;
int arr[n];
for(int i=0; i<n; i++) cin>> arr[i];
// Input the sum
int sum; cin >> sum;
cout<<subset_sum(arr,sum,n);
}


87 changes: 87 additions & 0 deletions Dynamic Programming/DifferentWaysToAddParenthesis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Problem can be found here : https://leetcode.com/problems/different-ways-to-add-parentheses/

Problem Statement :
Given a string expression of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. You may return the answer in any order.

Example 1:
Input: expression = "2-1-1"
Output: [0,2]
Explanation:
((2-1)-1) = 0
(2-(1-1)) = 2

Example 2:
Input: expression = "2*3-4*5"
Output: [-34,-14,-10,-10,10]
Explanation:
(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10



Approach :
The problem uses simple recursive approach
Placing brackets can be seen as evaluating expression for each operation
1+2-3+4 => (1+2)-(3+4) => placing bracket == evaluating left and right half and operating it with operator
1) loop over expression
2) if an operator is found : recur for left and right half to find all possible values that can be obtained from both side expressions standalone
3) for all values on left:
for all values on right:
operate with current operator and add result to ret vector


finally return the ret vector
4) to improve complexity : we memoize intermediate results in a map of string vs vector : stores the diff ways that can be there to put brackets in this expression
*/


#include <bits/stdc++.h>
using namespace std;

vector<int> dp(string str,map<string,vector<int>>&dpMap){
if (dpMap.count(str)>0)
return dpMap[str];

vector<int>ret;

//consider if ith char is operator then call for left and right half and evaluate all possible combinations
for (int i=0;i<str.length();++i){
if (str[i]=='+' or str[i]=='-' or str[i]=='*'){
vector<int>lef = dp(str.substr(0,i),dpMap),
right = dp(str.substr(i+1),dpMap);
// diff ways to solve left and right half

// now operate with possible operators
for (int& x1:lef) for (int& x2:right){
if (str[i]=='+') ret.emplace_back(x1+x2);
else if (str[i]=='-') ret.emplace_back(x1-x2);
else ret.emplace_back(x1*x2);
}
}
}

if (ret.empty()) // base case => totally numeric string
return {stoi(str)};

return ret;
}


vector<int> diffWaysToCompute(string expression) {
map<string,vector<int>>dpMap;
return dp(expression,dpMap);
}

int main(){
string s;
cin>>s;
vector<int> ans = diffWaysToCompute(s);
for (auto x:ans)
cout<<x<<" ";

return 0;
}
57 changes: 57 additions & 0 deletions Dynamic Programming/Equal_Sum_Partition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Equal Sum Partition
// Input :
// 4
// 1 5 11 5
// Output :
// true

#include<iostream>
using namespace std;
bool subset_sum(int arr[],int sum,int n)
{
bool t[n+1][sum+1];
for(int i=0;i<n+1;i++)
{
for(int j=0;j<sum+1;j++)
{
if(i==0)
t[i][j]=false;
if(j==0)
t[i][j]=true;
}
}
for(int i=1;i<n+1;i++)
{
for(int j=1;j<sum+1;j++)
{
if(arr[i-1]<=j)
t[i][j]=t[i-1][j-arr[i-1]]||t[i-1][j];
else
t[i][j]=t[i-1][j];
}
}
return t[n][sum];
}

int main()
{
int n; cin>>n;
int arr[n];
for(int i=0;i<n;i++) cin>>arr[i];
int s=0;
for(int i=0;i<n;i++)
{
s=s+arr[i];
}
if(s%2!=0)
std::cout << "false" << std::endl;
else
{
int flag=subset_sum(arr,s/2,n);
if(flag)
cout<<"true"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}
Loading