-
Notifications
You must be signed in to change notification settings - Fork 0
322. Coin Change #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
322. Coin Change #52
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
|
|
||
| Time Comprexity: O(n * m) (n: amount, m: coinsの長さ) | ||
| Space Complexity: O(n) | ||
|
|
||
| intでオーバーフローしてしまい、脊髄反射的にlongを使用したがDPの方向を変えればオーバーフローしなさそう | ||
| */ | ||
| class Solution { | ||
| public: | ||
| int coinChange(vector<int>& coins, int amount) { | ||
| vector<long> coin_nums(amount + 1, numeric_limits<long>::max()); | ||
| coin_nums[0] = 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメントありがとうございます、なるほど、 |
||
| for (long i = 0; i <= amount; ++i) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. long はデータモデルによってビット数が変わります。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメントありがとうございます、 |
||
| if (coin_nums[i] == numeric_limits<long>::max()) { | ||
| continue; | ||
| } | ||
| for (const long coin : coins) { | ||
| if (i + coin > amount) { | ||
| continue; | ||
| } | ||
| coin_nums[i + coin] = min(coin_nums[i] + 1, coin_nums[i + coin]); | ||
| } | ||
| } | ||
| if (coin_nums.back() == numeric_limits<long>::max()) { | ||
| return -1; | ||
| } | ||
| return coin_nums.back(); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| DPの方向を変えてオーバーフローしないようにした。 | ||
| */ | ||
|
|
||
| class Solution { | ||
| public: | ||
| int coinChange(vector<int>& coins, int amount) { | ||
| vector<int> coin_nums(amount + 1, numeric_limits<int>::max()); | ||
| coin_nums[0] = 0; | ||
| for (int to_coins = 1; to_coins <= amount; ++to_coins) { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| for (const int coin : coins) { | ||
| int from_coins = to_coins - coin; | ||
| if (from_coins < 0) { | ||
| continue; | ||
| } | ||
| if (coin_nums[from_coins] == numeric_limits<int>::max()) { | ||
| continue; | ||
| } | ||
| coin_nums[to_coins] = min(coin_nums[from_coins] + 1, coin_nums[to_coins]); | ||
| } | ||
| } | ||
| if (coin_nums.back() == numeric_limits<int>::max()) { | ||
| return -1; | ||
| } | ||
| return coin_nums.back(); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| class Solution { | ||
| public: | ||
| int coinChange(vector<int>& coins, int amount) { | ||
| vector<int> min_coins(amount + 1, numeric_limits<int>::max()); | ||
| min_coins[0] = 0; | ||
| for (int to_price = 1; to_price <= amount; ++to_price) { | ||
| for (const int coin : coins) { | ||
| int from_price = to_price - coin; | ||
| if (from_price < 0) { | ||
| continue; | ||
| } | ||
| if (min_coins[from_price] == numeric_limits<int>::max()) { | ||
| continue; | ||
| } | ||
| min_coins[to_price] = min(min_coins[from_price] + 1, min_coins[to_price]); | ||
| } | ||
| } | ||
| if (min_coins.back() == numeric_limits<int>::max()) { | ||
| return -1; | ||
| } | ||
| return min_coins.back(); | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらをご確認ください
Hiroto-Iizuka/coding_practice#26 (comment)