-
Notifications
You must be signed in to change notification settings - Fork 0
322. Coin Change #40
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 #40
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,71 @@ | ||
| ## 何も見ずに解いてみる | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int coinChange(vector<int>& coins, int amount) { | ||
| vector<int> viable_coins; | ||
| for (const int coin : coins) { | ||
| if (coin <= amount) { | ||
| viable_coins.push_back(coin); | ||
| } | ||
| } | ||
| vector<int> fewest_coins(amount + 1, -1); | ||
| fewest_coins[0] = 0; | ||
| for (int i = 0; i < amount; ++i) { | ||
| if (fewest_coins[i] != -1) { | ||
|
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. 真偽を反転して continue し、ネストを下げたほうが読みやすくなると思います。 |
||
| for (const int coin : viable_coins) { | ||
| if (i + coin <= amount) { | ||
| if (fewest_coins[i + coin] == -1) { | ||
|
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. fewest_coins を -1 ではなく大きな数字で初期化すると、 if 文が消せ、 fewest_coins[i + coin] = min(fewest_coins[i + coin], fewest_coins[i] + 1);だけで済むようになり、シンプルになると思います。 |
||
| fewest_coins[i + coin] = fewest_coins[i] + 1; | ||
| } else { | ||
| fewest_coins[i + coin] = min(fewest_coins[i + coin], fewest_coins[i] + 1); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return fewest_coins.back(); | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| 最初viable_coinsは作らずにやっていたのだが | ||
| ``` | ||
| 1 <= coins[i] <= 2^31 - 1 | ||
| 0 <= amount <= 10^4 | ||
| ``` | ||
| この不思議な制約によりi + coin <= amountを計算するときにオーバーフローになってしまった。 | ||
|
|
||
| ただ実際にはオーバーフローは結構気にかけないといけないなと思った。pythonでは気にしなくていいが他の言語では注意を回す癖をつけないといけない。 | ||
|
|
||
| ## 他の人のコードを見てみる | ||
|
|
||
| https://github.com/ryosuketc/leetcode_arai60/pull/53/files | ||
| https://github.com/TORUS0818/leetcode/pull/42/files | ||
| https://github.com/tokuhirat/LeetCode/pull/40/files | ||
| https://github.com/hroc135/leetcode/pull/38/files | ||
| https://github.com/goto-untrapped/Arai60/pull/34/files | ||
|
|
||
| 「一つの変数に二つ以上の意味が含まれてるのはわかりにくい」というのは書いているときにも思った。 これを解決するためにintの上限などを持ち出すかは迷うところ・・・ | ||
|
|
||
| ## 最終コード | ||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int coinChange(vector<int>& coins, int amount) { | ||
| vector<int> min_coins(amount + 1, -1); | ||
|
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. 枚数としてあり得ない大きい数で初期化したい派です。 |
||
| min_coins[0] = 0; | ||
| for (int 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. i よりは合計金額という意味がわかる変数名だとわかりやすいと思いました。
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. 本当はamountという変数名をつけたかったんですが、もともとの関数の引数の名前に取られてたのでiにしました。 class Solution {
public:
int coinChange(vector<int>& coins, int target_amount) {
vector<int> min_coins(target_amount + 1, -1);
min_coins[0] = 0;
for (int amount = 0; amount <= target_amount; ++amount) {
if (min_coins[amount] == -1) continue;
for (const int coin : coins) {
if (coin > target_amount - amount) continue;
min_coins[amount + coin] = min_coins[amount + coin] == -1
? min_coins[amount] + 1
: min(min_coins[amount + coin], min_coins[amount] + 1);
}
}
return min_coins.back();
}
}; |
||
| if (min_coins[i] == -1) continue; | ||
| for (const int coin : coins) { | ||
| if (coin > amount - i) continue; | ||
| min_coins[i + coin] = min_coins[i + coin] == -1 | ||
| ? min_coins[i] + 1 | ||
| : min(min_coins[i + coin], min_coins[i] + 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.
範囲 for 文でプリミティブ型変数で回すときは、 const は付けないことが多いように思います。好みの問題かもしれません。