code >>
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
57
58
59
|
class Solution {
public:
int returnMin(vector<int>& funcSave, vector<int>& tempVec) {
vector<int> temporary;
for (const auto& temp : tempVec)
if (temp < 0)
continue;
else
if (funcSave[temp] >= 0)
temporary.push_back(funcSave[temp]);
else
continue;
if (temporary.size() > 0)
{
int min = *min_element(temporary.begin(), temporary.end());
return min;
}
else
return -1;
}
int coinChange(vector<int>& coins, int amount) {
sort(coins.begin(), coins.end());
int vecSize = (int)coins.size();
if (amount == 0)
return 0;
if (coins[0] > amount)
return -1;
if (vecSize == 1)
{
if (amount % coins[0] == 0)
return amount / coins[0];
else
return -1;
}
vector<int> funcSave;
funcSave.emplace_back(0);
int coinFront = coins[0];
int ans = 0;
for (int i = 1; i < coinFront; i++)
funcSave.emplace_back(-1);
for (int i = coinFront; i <= amount; i++)
{
vector<int> tempVec;
for (int j = 0; j < vecSize; j++)
{
tempVec.emplace_back(i - coins[j]);
}
ans = returnMin(funcSave, tempVec);
if (ans < 0)
funcSave.emplace_back(ans);
else
funcSave.emplace_back(ans + 1);
}
return funcSave.back();
}
};
|
'C' 카테고리의 다른 글
c++ opencv keypoint를 활용한 homography 찾기 (0) | 2021.08.13 |
---|---|
영상처리 C++ 코드 - HOG+SVM을 활용한 보행자 검출 (0) | 2021.08.12 |
leetcode minimum path sum c++ code (0) | 2021.07.27 |
c++ leetcode DP min cost climing stairs code (0) | 2021.07.26 |
c++ leetcode climbing stairs DP solution(Top-down and bottom-up) (0) | 2021.07.24 |