Skip to content

Commit

Permalink
dp
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinwkt committed Sep 12, 2018
1 parent 13b6e45 commit e0694c1
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 0 deletions.
Binary file added array/.sortRotated.cpp.swp
Binary file not shown.
22 changes: 22 additions & 0 deletions dp/01knapsack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <vector>

using namespace std;

int max(int a, int b){return a>b?a:b;}

int ks(vector<int> &val, vector<int> &wt, int maxW){
vector<int> dp(maxW+1,0);
for(int i=0;i<val.size();i++) for(int j=maxW;j>=wt[i];j--) dp[j]=max(dp[j],dp[j-wt[i]]+val[i]);
return dp[maxW];
}

int main() {
vector<int> val = {60, 100, 120};
vector<int> wt = {10, 20, 30};
int maxW = 50;

printf("%d\n",ks(val,wt,maxW));

return 0;
}
Binary file added dp/a.out
Binary file not shown.
29 changes: 29 additions & 0 deletions dp/coinChangeMinFinite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <vector>
#include <limits.h>

using namespace std;

int min(int a, int b){return a<b?a:b;}

int coin(vector<int> &v, int target){
vector<int> dp(target+1,INT_MAX-1);
for(int ii=1;ii<=target;ii++) printf("%d ",dp[ii]);
printf("\n");
dp[0]=0;
for(int i=0;i<v.size();i++){
for(int j=target;j>=v[i];j--){
dp[j]=min(dp[j],dp[j-v[i]]+1);
}
for(int ii=1;ii<=target;ii++) printf("%d ",dp[ii]);
printf("\n");
}
return dp[target];
}

int main(){
vector<int> v={1,5,6,8};
int target= 11;
printf("%d\n",coin(v,target));
return 0;
}
21 changes: 21 additions & 0 deletions dp/coinChangeMinInfinite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include <vector>
#include <limits.h>

using namespace std;

int min(int a, int b){return a<b?a:b;}

int coin(vector<int> &v, int target){
vector<int> dp(target+1,INT_MAX-1);
dp[0]=0;
for(int i=0;i<v.size();i++) for(int j=v[i];j<=target;j++) dp[j]=min(dp[j],dp[j-v[i]]+1);
return dp[target];
}

int main(){
vector<int> v={1,5,6,8};
int target= 11;
printf("%d\n",coin(v,target));
return 0;
}
21 changes: 21 additions & 0 deletions dp/lcs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int lcs(string a, string b){
vector<vector<int>> dp(a.length()+1,vector<int>(b.length()+1,0));
for(int i=1;i<=a.length();i++){
for(int j=1;j<=b.length();j++){
if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
return dp[a.length()][b.length()];
}

int main(){
printf("%d\n",lcs("abcdaf","acbcf"));
return 0;
}
31 changes: 31 additions & 0 deletions dp/subsetSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>
#include <vector>

using namespace std;

bool subset(vector<int> &v, int target){
vector<vector<bool> > dp(v.size()+1,vector<bool>(target+1,1));
for(int i=0;i<=v.size();i++){
for(int j=0;j<=target;j++){
if(j==0) dp[i][j]=1;
else if(i==0) dp[i][j]=0;
else if(j-v[i-1]>=0) dp[i][j]=dp[i-1][j]||dp[i-1][j-v[i-1]];
else dp[i][j]=dp[i-1][j];
}
}

for(int i=1;i<=v.size();i++){
for(int j=1;j<=target;j++){
printf("%3d",dp[i][j]? 1:0);
}
printf("\n");
}
return dp[v.size()][target];
}

int main(){
vector<int> v = {2,3,7,8,10};
int target = 32;
printf("%d\n",subset(v,target)? 1:0);
return 0;
}

0 comments on commit e0694c1

Please sign in to comment.