Skip to content

Commit 63ddb7c

Browse files
committed
feat: Add solution for LeetCode problem 322
1 parent 2f0e23e commit 63ddb7c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

coin-change/WhiteHyun.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// 322. Coin Change
3+
// https://leetcode.com/problems/coin-change/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/14.
7+
//
8+
9+
class Solution {
10+
func coinChange(_ coins: [Int], _ amount: Int) -> Int {
11+
var dp: [Int] = .init(repeating: .max, count: amount + 1)
12+
13+
dp[0] = 0
14+
15+
for coin in coins {
16+
for index in stride(from: coin, to: dp.count, by: 1) where dp[index - coin] != .max && dp[index - coin] + 1 < dp[index] {
17+
dp[index] = dp[index - coin] + 1
18+
}
19+
}
20+
21+
return dp.last == .max ? -1 : dp.last!
22+
}
23+
}

0 commit comments

Comments
 (0)