-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec10_67.cpp
More file actions
46 lines (31 loc) · 1.13 KB
/
Copy pathlec10_67.cpp
File metadata and controls
46 lines (31 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
int f(int ind , int T , vector<int>&a , vector<vector<int>>&dp){
if( ind ==0 ){
return (T % a[0] == 0 );
}
if(dp[ind][T]!=-1) return dp[ind][T];
int notTake = f(ind -1 , T , a,dp);
int take = 0 ;
if(a[ind]<=T) take = f(ind , T - a[ind] ,a,dp);
return dp[ind][T] = take + notTake ;
}
int change(int amount, vector<int>& a) {
int n = a.size();
vector<vector<int>> dp(n, vector<int>(amount+1, 0));
vector<int>prev(amount+1 , 0) , cur(amount +1 , 0);
for(int T = 0 ; T <= amount;T++){
prev[T] = ( T% a[0] == 0);
}
for(int ind = 1 ; ind < n; ind++){
for(int T = 0 ; T<=amount ; T++){
int notTake = prev[T];
int take = 0 ;
if(a[ind]<=T) take = cur[T - a[ind] ];
cur[T] = take + notTake ;
}
prev = cur;
}
return prev[ amount ];
}
};