Skip to content

Commit 56c0dc5

Browse files
committed
Practise 07-Jul-2020
1 parent 27d973a commit 56c0dc5

File tree

8 files changed

+408
-44
lines changed

8 files changed

+408
-44
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
2+
3+
4+
5+
Example 1:
6+
7+
Input: amount = 5, coins = [1, 2, 5]
8+
Output: 4
9+
Explanation: there are four ways to make up the amount:
10+
5=5
11+
5=2+2+1
12+
5=2+1+1+1
13+
5=1+1+1+1+1
14+
Example 2:
15+
16+
Input: amount = 3, coins = [2]
17+
Output: 0
18+
Explanation: the amount of 3 cannot be made up just with coins of 2.
19+
Example 3:
20+
21+
Input: amount = 10, coins = [10]
22+
Output: 1
23+
24+
25+
Note:
26+
27+
You can assume that
28+
29+
0 <= amount <= 5000
30+
1 <= coin <= 5000
31+
the number of coins is less than 500
32+
the answer is guaranteed to fit into signed 32-bit integer
33+
34+
35+
36+
37+
38+
39+
class Solution {
40+
public:
41+
42+
int change(int amount, vector<int>& coins) {
43+
int n=coins.size();
44+
int dp[n+1][amount+1];
45+
46+
for(int i=0;i<n+1;i++) dp[i][0]=1;
47+
for(int j=1;j<amount+1;j++) dp[0][j]=0;
48+
49+
for(int i=1;i<n+1;i++){
50+
for(int j=1;j<amount+1;j++){
51+
if(coins[i-1]<=j){
52+
dp[i][j] = dp[i][j-coins[i-1]] + dp[i-1][j];
53+
} else {
54+
dp[i][j] = dp[i-1][j];
55+
}
56+
}
57+
}
58+
return dp[n][amount];
59+
}
60+
};
61+
62+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
2+
3+
4+
5+
Example 1:
6+
7+
Input: amount = 5, coins = [1, 2, 5]
8+
Output: 4
9+
Explanation: there are four ways to make up the amount:
10+
5=5
11+
5=2+2+1
12+
5=2+1+1+1
13+
5=1+1+1+1+1
14+
Example 2:
15+
16+
Input: amount = 3, coins = [2]
17+
Output: 0
18+
Explanation: the amount of 3 cannot be made up just with coins of 2.
19+
Example 3:
20+
21+
Input: amount = 10, coins = [10]
22+
Output: 1
23+
24+
25+
Note:
26+
27+
You can assume that
28+
29+
0 <= amount <= 5000
30+
1 <= coin <= 5000
31+
the number of coins is less than 500
32+
the answer is guaranteed to fit into signed 32-bit integer
33+
34+
35+
36+
37+
38+
class Solution {
39+
public:
40+
41+
int change(int amount, vector<int>& coins) {
42+
int n=coins.size();
43+
int dp[n+1][amount+1];
44+
45+
for(int i=0;i<n+1;i++) dp[i][0]=1;
46+
for(int j=1;j<amount+1;j++) dp[0][j]=0;
47+
48+
for(int i=1;i<n+1;i++){
49+
for(int j=1;j<amount+1;j++){
50+
if(coins[i-1]<=j){
51+
dp[i][j] = dp[i][j-coins[i-1]] + dp[i-1][j];
52+
} else {
53+
dp[i][j] = dp[i-1][j];
54+
}
55+
}
56+
}
57+
return dp[n][amount];
58+
}
59+
};
60+
61+

dynamic programming/01_knapsack.cpp

+46-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,50 @@
1+
/*
2+
You are given weights and values of N items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. Note that we have only one quantity of each item.
3+
In other words, given two integer arrays val[0..N-1] and wt[0..N-1] which represent values and weights associated with N items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it (0-1 property).
4+
5+
Input:
6+
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of four lines.
7+
The first line consists of N the number of items.
8+
The second line consists of W, the maximum capacity of the knapsack.
9+
In the next line are N space separated positive integers denoting the values of the N items,
10+
and in the fourth line are N space separated positive integers denoting the weights of the corresponding items.
11+
12+
Output:
13+
For each testcase, in a new line, print the maximum possible value you can get with the given conditions that you can obtain for each test case in a new line.
14+
15+
Constraints:
16+
1 ≤ T ≤ 100
17+
1 ≤ N ≤ 1000
18+
1 ≤ W ≤ 1000
19+
1 ≤ wt[i] ≤ 1000
20+
1 ≤ v[i] ≤ 1000
21+
22+
Example:
23+
Input:
24+
2
25+
3
26+
4
27+
1 2 3
28+
4 5 1
29+
3
30+
3
31+
1 2 3
32+
4 5 6
33+
Output:
34+
3
35+
0
36+
Explanation:
37+
Test Case 1: With W = 4, you can either choose the 0th item or the 2nd item. Thus, the maximum value you can generate is the max of val[0] and val[2], which is equal to 3.
38+
Test Case 2: With W = 3, there is no item you can choose from the given list as all the items have weight greater than W. Thus, the maximum value you can generate is 0.
39+
*/
40+
41+
42+
43+
144
#include<bits/stdc++.h>
245
using namespace std;
346

4-
int dp[100][1000];
47+
int dp[100][1000]; // Set according to the constraints provided n, w
548

649
int maxi(int a, int b) { return (a > b)? a : b; }
750

@@ -24,8 +67,9 @@ int main(){
2467
int n,w;
2568
cin>>n>>w;
2669
int wt[n],val[n];
27-
for(int i=0;i<n;i++) cin>>wt[i];
2870
for(int i=0;i<n;i++) cin>>val[i];
71+
for(int i=0;i<n;i++) cin>>wt[i];
72+
2973
cout<<knapsack(wt,val,w,n)<<endl;
3074
}
3175
return 0;

dynamic programming/01_knapsack_dp.cpp

-40
This file was deleted.

dynamic programming/01_knapsack_memoization.cpp

+48-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,53 @@
1+
/*
2+
You are given weights and values of N items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. Note that we have only one quantity of each item.
3+
In other words, given two integer arrays val[0..N-1] and wt[0..N-1] which represent values and weights associated with N items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it (0-1 property).
4+
5+
Input:
6+
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of four lines.
7+
The first line consists of N the number of items.
8+
The second line consists of W, the maximum capacity of the knapsack.
9+
In the next line are N space separated positive integers denoting the values of the N items,
10+
and in the fourth line are N space separated positive integers denoting the weights of the corresponding items.
11+
12+
Output:
13+
For each testcase, in a new line, print the maximum possible value you can get with the given conditions that you can obtain for each test case in a new line.
14+
15+
Constraints:
16+
1 ≤ T ≤ 100
17+
1 ≤ N ≤ 1000
18+
1 ≤ W ≤ 1000
19+
1 ≤ wt[i] ≤ 1000
20+
1 ≤ v[i] ≤ 1000
21+
22+
Example:
23+
Input:
24+
2
25+
3
26+
4
27+
1 2 3
28+
4 5 1
29+
3
30+
3
31+
1 2 3
32+
4 5 6
33+
Output:
34+
3
35+
0
36+
Explanation:
37+
Test Case 1: With W = 4, you can either choose the 0th item or the 2nd item. Thus, the maximum value you can generate is the max of val[0] and val[2], which is equal to 3.
38+
Test Case 2: With W = 3, there is no item you can choose from the given list as all the items have weight greater than W. Thus, the maximum value you can generate is 0.
39+
*/
40+
41+
42+
43+
44+
45+
46+
147
#include<bits/stdc++.h>
248
using namespace std;
349

4-
int dp[100][1000];
50+
int dp[100][1000]; // Set according to the constraints provided n, w
551

652
int maxi(int a, int b) { return (a > b)? a : b; }
753

@@ -24,8 +70,8 @@ int main(){
2470
int n,w;
2571
cin>>n>>w;
2672
int wt[n],val[n];
27-
for(int i=0;i<n;i++) cin>>wt[i];
2873
for(int i=0;i<n;i++) cin>>val[i];
74+
for(int i=0;i<n;i++) cin>>wt[i];
2975
cout<<knapsack(wt,val,w,n)<<endl;
3076
}
3177
return 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
You are given weights and values of N items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. Note that we have only one quantity of each item.
3+
In other words, given two integer arrays val[0..N-1] and wt[0..N-1] which represent values and weights associated with N items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it (0-1 property).
4+
5+
Input:
6+
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of four lines.
7+
The first line consists of N the number of items.
8+
The second line consists of W, the maximum capacity of the knapsack.
9+
In the next line are N space separated positive integers denoting the values of the N items,
10+
and in the fourth line are N space separated positive integers denoting the weights of the corresponding items.
11+
12+
Output:
13+
For each testcase, in a new line, print the maximum possible value you can get with the given conditions that you can obtain for each test case in a new line.
14+
15+
Constraints:
16+
1 ≤ T ≤ 100
17+
1 ≤ N ≤ 1000
18+
1 ≤ W ≤ 1000
19+
1 ≤ wt[i] ≤ 1000
20+
1 ≤ v[i] ≤ 1000
21+
22+
Example:
23+
Input:
24+
2
25+
3
26+
4
27+
1 2 3
28+
4 5 1
29+
3
30+
3
31+
1 2 3
32+
4 5 6
33+
Output:
34+
3
35+
0
36+
Explanation:
37+
Test Case 1: With W = 4, you can either choose the 0th item or the 2nd item. Thus, the maximum value you can generate is the max of val[0] and val[2], which is equal to 3.
38+
Test Case 2: With W = 3, there is no item you can choose from the given list as all the items have weight greater than W. Thus, the maximum value you can generate is 0.
39+
*/
40+
41+
42+
43+
44+
#include<bits/stdc++.h>
45+
using namespace std;
46+
47+
48+
int maxi(int a, int b) { return (a > b)? a : b; }
49+
50+
int knapsack(int wt[], int val[], int w, int n){
51+
if(n==0 || w==0) return 0;
52+
if(wt[n-1]<=w)
53+
return maxi(val[n-1]+knapsack(wt,val,w-wt[n-1],n-1), knapsack(wt,val,w,n-1));
54+
else return knapsack(wt,val,w,n-1);
55+
}
56+
57+
int main(){
58+
ios_base::sync_with_stdio(false);
59+
cin.tie(NULL);
60+
cout.tie(NULL);
61+
int t;
62+
cin>>t;
63+
while(t--){
64+
int n,w;
65+
cin>>n>>w;
66+
int wt[n],val[n];
67+
for(int i=0;i<n;i++) cin>>val[i];
68+
for(int i=0;i<n;i++) cin>>wt[i];
69+
70+
cout<<knapsack(wt,val,w,n)<<endl;
71+
}
72+
return 0;
73+
}

0 commit comments

Comments
 (0)