-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2294.cpp
More file actions
69 lines (60 loc) · 1.1 KB
/
2294.cpp
File metadata and controls
69 lines (60 loc) · 1.1 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// ³ªÀÇ Ç®ÀÌ
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int MAX = 100000;
int d[MAX];
int n, k;
int* coins;
int dp(int num) {
for (int i = 0; i < n; ++i) {
if (num == coins[i]) return 1;
}
if (d[num] != 0) return d[num];
int result = MAX;
for (int i = 0; i < n; ++i) {
int tmp = num - coins[i];
if (tmp > 0) {
result = min(dp(tmp) + 1, result);
}
}
return d[num] = result;
}
int main() {
cin >> n >> k;
coins = new int[n];
for (int i = 0; i < n; ++i) {
cin >> coins[i];
}
int answer = dp(k);
if (answer == MAX) cout << -1;
else cout << answer;
}
// ´Ù¸¥ »ç¶÷ Ç®ÀÌ
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int MAX = 100000;
int main() {
int d[MAX] = { 0, };
int n, k;
int* coins;
cin >> n >> k;
coins = new int[n];
for (int i = 0; i < n; ++i) {
cin >> coins[i];
}
for (int i = 0; i <= k; i++) {
d[0] = 0;
d[i] = MAX;
}
for (int i = 0; i < n; i++) {
for (int j = coins[i]; j <= k; j++) {
d[j] = min(d[j], d[j - coins[i]] + 1);
}
}
if (d[k] == MAX) cout << -1;
else cout << d[k];
}