Skip to content

Commit 5bc8437

Browse files
committed
feat: add solution for coin change in rust
1 parent bd5f7a5 commit 5bc8437

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/medium/coin_change.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![allow(dead_code)]
2+
pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
3+
let mut dp = vec![amount + 1; (amount + 1) as usize];
4+
dp[0] = 0;
5+
6+
for a in 1..=amount {
7+
for &c in &coins {
8+
if c <= a {
9+
dp[a as usize] = dp[a as usize].min(dp[(a - c) as usize] + 1);
10+
}
11+
}
12+
}
13+
14+
if dp[amount as usize] > amount {
15+
-1
16+
} else {
17+
dp[amount as usize]
18+
}
19+
}
20+
21+
#[test]
22+
fn test_coin_change() {
23+
assert_eq!(coin_change(vec![1, 2, 5], 11), 3);
24+
assert_eq!(coin_change(vec![2], 3), -1);
25+
assert_eq!(coin_change(vec![1], 0), 0);
26+
assert_eq!(coin_change(vec![1], 1), 1);
27+
assert_eq!(coin_change(vec![1], 2), 2);
28+
}

src/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
- [x] [238. Product of array except self](../src/medium/product_of_array_except_self.rs) -> [Problem Description](../src/medium/readme.md#238-product-of-array-except-self)
8989
- [x] [271. Encode and decode strings](../src/medium/encode_and_decode_strings.rs) -> [Problem Description](../src/medium/readme.md#271-encode-and-decode-strings)
9090
- [x] [287. Find the duplicate number](../src/medium/find_the_duplicate_number.rs) -> [Problem Description](../src/medium/readme.md#287-find-the-duplicate-number)
91-
- [ ] [322. Coin change](../src/medium/coin_change.rs) -> [Problem Description](../src/medium/readme.md#322-coin-change)
91+
- [x] [322. Coin change](../src/medium/coin_change.rs) -> [Problem Description](../src/medium/readme.md#322-coin-change)
9292
- [x] [347. Top k frequent elements](../src/medium/top_k_frequent_elements.rs) -> [Problem Description](../src/medium/readme.md#347-top-k-frequent-elements)
9393
- [x] [355. Design twitter](../src/medium/design_twitter.rs) -> [Problem Description](../src/medium/readme.md#355-design-twitter)
9494
- [x] [424. Longest repeating character replacement](../src/medium/longest_repeating_character_replacement.rs) -> [Problem Description](../src/medium/readme.md#424-longest-repeating-character-replacement)

theory/categories/6.1d_dp/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Dynamic programming and greedy algorithms are similar. Both are optimization met
2626
- [x] [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | Medium | [Solution](../../../src/medium/longest_palindromic_substring.rs) | [Problem Description](../../../src/medium/readme.md#5-longest-palindromic-substring)
2727
- [x] [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | Medium | [Solution](../../../src/medium/palindromic_substrings.rs) | [Problem Description](../../../src/medium/readme.md#647-palindromic-substrings)
2828
- [x] [Decode Ways](https://leetcode.com/problems/decode-ways/) | Medium | [Solution](../../../src/medium/decode_ways.rs) | [Problem Description](../../../src/medium/readme.md#91-decode-ways)
29-
- [ ] [Coin Change](https://leetcode.com/problems/coin-change/) | Medium | [Solution](../../../src/medium/coin_change.rs) | [Problem Description](../../../src/medium/readme.md#322-coin-change)
29+
- [x] [Coin Change](https://leetcode.com/problems/coin-change/) | Medium | [Solution](../../../src/medium/coin_change.rs) | [Problem Description](../../../src/medium/readme.md#322-coin-change)
3030
- [x] [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | Medium | [Solution](../../../src/medium/maximum_product_subarray.rs) | [Problem Description](../../../src/medium/readme.md#152-maximum-product-subarray)
3131
- [ ] [Word Break](https://leetcode.com/problems/word-break/) | Medium | [Solution](../../../src/medium/word_break.rs) | [Problem Description](../../../src/medium/readme.md#139-word-break)
3232
- [ ] [Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) | Medium | [Solution](../../../src/medium/longest_increasing_subsequence.rs) | [Problem Description](../../../src/medium/readme.md#300-longest-increasing-subsequence)

0 commit comments

Comments
 (0)