Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add standard dp questions #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
51 changes: 51 additions & 0 deletions src/coin change - minimum number of coins.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//https://practice.geeksforgeeks.org/problems/number-of-coins/0/

//2D dp
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int sum,n;
cin>>sum>>n;
vector<int> v(n);
for(auto &itr:v) cin>>itr;
int dp[n+1][sum+1];
for(int j=0;j<=sum;j++) dp[0][j] = INT_MAX - 1;
for(int i=0;i<=n;i++) dp[i][0] = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=sum;j++){
if(j>=v[i-1]){
dp[i][j] = min(1 + dp[i][j-v[i-1]],dp[i-1][j]);
}
else dp[i][j] = dp[i-1][j];
}
}
if(dp[n][sum] == INT_MAX - 1) cout<<-1<<endl;
else
cout<<dp[n][sum]<<endl;
}
return 0;
}


//1D dp

ll n,sum;
cin>>n>>sum;
vector<ll> v(n);
for(auto &itr:v) cin>>itr;
ll dp[sum+1];
for(ll i=0;i<=sum;i++) dp[i] = INT_MAX-1;
dp[0]=0;

for(ll i=1;i<=sum;i++){
for(ll j=1;j<=n;j++){
if(i>=v[j-1])
dp[i] = min(1+ dp[i-v[j-1]] , dp[i]);
}
}
if(dp[sum]==INT_MAX-1) cout<<-1;
else
cout<<dp[sum];
29 changes: 29 additions & 0 deletions src/coin change - number of ways.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// https://practice.geeksforgeeks.org/problems/coin-change/0

#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int sum;
vector<int> v(n);
for(auto &itr:v) cin>>itr;
cin>>sum;
int dp[n+1][sum+1];
for(int j=0;j<=sum;j++) dp[0][j]=0;
for(int i=0;i<=n;i++) dp[i][0]=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=sum;j++){
if(j>=v[i-1]){
dp[i][j] = dp[i][j-v[i-1]] + dp[i-1][j];
}
else dp[i][j] = dp[i-1][j];
}
}
cout<<dp[n][sum]<<endl;
}
return 0;
}
42 changes: 42 additions & 0 deletions src/count number of subsets with sum = k.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include<bits/stdc++.h>
using namespace std;
vector<int> v;
int n;
int main(){
int t;
cin>>t;
while(t--){
v.clear();
cin>>n;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
v.push_back(temp);
}
int k;
cin>>k;
int sum=k;
int dp[n+5][sum+5];

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

if((j-v[i-1])>=0){
dp[i][j] = (dp[i-1][j-v[i-1]] + dp[i-1][j]);
//only change or of subset sum to + here to count the number of subsets


}
else{
dp[i][j] = dp[i-1][j];
}
}
}
cout<<dp[n][sum]<<endl;

}
return 0;
}
40 changes: 40 additions & 0 deletions src/minimum subset sum difference iterative.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//https://practice.geeksforgeeks.org/problems/minimum-sum-partition/0

#include<bits/stdc++.h>

using namespace std;

int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<int> v(n);
for(auto &itr:v) cin>>itr;
int fullsum=0;
for(auto ele:v) fullsum+=ele;

int sum=fullsum/2;
bool dp[n+5][(sum)+5];
for(int j=0;j<=sum;j++) dp[0][j]=false;
for(int i=0;i<=n;i++) dp[i][0]=true;

for(int i=1;i<=n;i++){
for(int j=1;j<=sum;j++){
if(j-v[i-1]>=0){
dp[i][j] = dp[i-1][j-v[i-1]] or dp[i-1][j];
}
else dp[i][j] = dp[i-1][j];
}
}
int s1;
for(int j=0;j<=sum;j++){
if(dp[n][j]) s1=j;
}

cout<<(fullsum - s1) - s1<<endl;
}
return 0;

}
30 changes: 30 additions & 0 deletions src/minimum subset sum difference recursive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//https://practice.geeksforgeeks.org/problems/minimum-sum-partition/0

#include<bits/stdc++.h>

using namespace std;

int dp[55][10001];
int arr[200001];
int solve(int ind,int x,int y,int n){
if(ind==n) return abs(x-y);

if(dp[ind][y]!=-1) return dp[ind][y];

return dp[ind][y]=min(solve(ind+1,x+arr[ind],y,n),solve(ind+1,x,y+arr[ind],n));

}


int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++) cin>>arr[i];
memset(dp,-1,sizeof(dp));
cout<<solve(0,0,0,n)<<endl;
}
return 0;
}
27 changes: 27 additions & 0 deletions src/reach a given score by some numbers distinctly.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://practice.geeksforgeeks.org/problems/reach-a-given-score/0

#include<bits/stdc++.h>
using namespace std;
int dp[1001][10];
int solve(int arr[],int n,int i){
if(i<0 or n<0) return 0;
if(n==0) return 1;
if(dp[n][i]!=-1) return dp[n][i];

return dp[n][i] = solve(arr,n-arr[i],i) + solve(arr,n,i-1);

}

int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
memset(dp,-1,sizeof(dp));
int arr[3] = {3,5,10};
cout<<solve(arr,n,2)<<endl;
}

return 0;
}
44 changes: 44 additions & 0 deletions src/subset sum - dp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<bits/stdc++.h>
using namespace std;
vector<int> v;
int n;
int main(){
int t;
cin>>t;
while(t--){
v.clear();
cin>>n;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
v.push_back(temp);
}
int sum=0;
for(auto ele:v) sum+=ele;
if(sum&1) {
cout<<"NO"<<endl;
continue;
}
sum/=2;
bool dp[n+5][sum+5];
memset(dp,false,sizeof(dp));
for(int j=0;j<=sum;j++) dp[0][j]=false;
for(int i=0;i<=n;i++) dp[i][0]=false;
for(int i=1;i<=n;i++){
for(int j=1;j<=sum;j++){
if((j-v[i-1])==0) dp[i][j] = true;
else if((j-v[i-1])>0){
dp[i][j] = (dp[i-1][j-v[i-1]] | dp[i-1][j]);
}
else{
dp[i][j] = dp[i-1][j];
}
}
}

if(dp[n][sum]) cout<<"YES"<<endl;
else cout<<"NO"<<endl;

}
return 0;
}
32 changes: 32 additions & 0 deletions src/unbounded Knapsack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<bits/stdc++.h>
using namespace std;

int main(){
int t;
cin>>t;
while(t--){
int n,w;
cin>>n>>w;

vector<int> v(n);
for(auto &itr:v) cin>>itr;
vector<int> wt(n);
for(auto &itr:wt) cin>>itr;
int dp[n+5][w+5];
for(int i=0;i<=n;i++) dp[i][0]=0;
for(int j=0;j<=w;j++) dp[0][j]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=w;j++){
if(j>=wt[i-1]){
//only one change from 0/1 Knapsack which is that if we take the item then we can again take it so v[i-1] + dp[i][j-wt[i-1]]
// i.e. in 0/1 Knapsack -- if 5,6,4,8 is the value array we can take 8 and move to arr with end 4 so dp[i-1][j-wt[i-1]]
// in Unbounded Knapsack -- if 5,6,4,8 is the value array we can take 8 and stay at arr with end 8 as again we can take 8 so dp[i][j-wt[i-1]]
dp[i][j] = max((v[i-1] + dp[i][j-wt[i-1]]),dp[i-1][j]);
}
else dp[i][j] = dp[i-1][j];
}
}
cout<<dp[n][w]<<endl;
}
return 0;
}