forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
56 lines (40 loc) · 1.26 KB
/
main.cpp
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
47
48
49
50
51
52
53
54
55
56
/// Source : https://leetcode.com/problems/domino-and-tromino-tiling/description/
/// Author : liuyubobobo
/// Time : 2018-03-08
#include <iostream>
#include <vector>
using namespace std;
/// Memory Search
///
/// Time Complexity: O(len(coins)*amount)
/// Space Complexity: O(len(coins)*amount)
class Solution {
private:
vector<vector<int>> dp;
public:
int change(int amount, vector<int>& coins) {
dp = vector<vector<int>>(coins.size(), vector<int>(amount + 1, -1));
int res = search(coins, 0, amount);
return res == -1 ? 0 : res;
}
private:
int search(const vector<int>& coins, int index, int amount){
if(index == coins.size())
return amount == 0 ? 1 : 0;
if(dp[index][amount] != -1)
return dp[index][amount];
int res = 0;
for(int i = 0 ; i * coins[index] <= amount ; i ++)
res += search(coins, index + 1, amount - i * coins[index]);
return dp[index][amount] = res;
}
};
int main() {
vector<int> coins1 = {1, 2, 5};
cout << Solution().change(5, coins1) << endl;
vector<int> coins2 = {2};
cout << Solution().change(3, coins2) << endl;
vector<int> coins3 = {10};
cout << Solution().change(10, coins3) << endl;
return 0;
}