Skip to content

Commit 5abd700

Browse files
committed
[Gold V] Title: 동전 2, Time: 24 ms, Memory: 69276 KB -BaekjoonHub
1 parent 780547a commit 5abd700

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [Gold V] 동전 2 - 2294
2+
3+
[문제 링크](https://www.acmicpc.net/problem/2294)
4+
5+
### 성능 요약
6+
7+
메모리: 69276 KB, 시간: 24 ms
8+
9+
### 분류
10+
11+
다이나믹 프로그래밍
12+
13+
### 제출 일자
14+
15+
2025년 2월 21일 18:56:52
16+
17+
### 문제 설명
18+
19+
<p>n가지 종류의 동전이 있다. 이 동전들을 적당히 사용해서, 그 가치의 합이 k원이 되도록 하고 싶다. 그러면서 동전의 개수가 최소가 되도록 하려고 한다. 각각의 동전은 몇 개라도 사용할 수 있다.</p>
20+
21+
### 입력
22+
23+
<p>첫째 줄에 n, k가 주어진다. (1 ≤ n ≤ 100, 1 ≤ k ≤ 10,000) 다음 n개의 줄에는 각각의 동전의 가치가 주어진다. 동전의 가치는 100,000보다 작거나 같은 자연수이다. 가치가 같은 동전이 여러 번 주어질 수도 있다.</p>
24+
25+
### 출력
26+
27+
<p>첫째 줄에 사용한 동전의 최소 개수를 출력한다. 불가능한 경우에는 -1을 출력한다.</p>
28+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let nk = readLine()!.split { $0 == " " }.map { Int(String($0))! }
2+
let coinList = (0..<nk[0]).map { _ in Int(readLine()!)! }.reduce(into: Set<Int>()){ $0.insert($1) }.sorted { $0 < $1 }
3+
var dp = [0] + [Int](repeating: 987654321, count: 10000)
4+
for coin in coinList {
5+
for i in stride(from: coin, through: nk[1], by: 1) {
6+
dp[i] = min(dp[i], dp[i-coin] + 1)
7+
}
8+
}
9+
print(dp[nk[1]] == 987654321 ? -1 : dp[nk[1]])

0 commit comments

Comments
 (0)