Skip to content

Commit 4f5c769

Browse files
authored
coin change solution
1 parent c1769eb commit 4f5c769

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

coin-change/yhkee0404.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
int coinChange(List<int> coins, int amount) {
3+
List<int> dp = List.filled(amount + 1, amount + 1);
4+
// S(n) = O(amount)
5+
dp[0] = 0;
6+
for (int i = 1; i <= amount; i++) {
7+
// T(n) = O(amount * coins.length)
8+
dp[i] = 1 + coins.where((x) => x <= i)
9+
.map((x) => dp[i - x])
10+
.fold(amount, (a, b) => min(a, b));
11+
}
12+
return dp[amount] <= amount ? dp[amount] : -1;
13+
}
14+
}

0 commit comments

Comments
 (0)