-
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
Conversation
| public: | ||
| int coinChange(vector<int>& coins, int amount) { | ||
| vector<int> viable_coins; | ||
| for (const int coin : coins) { |
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 は付けないことが多いように思います。好みの問題かもしれません。
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
真偽を反転して continue し、ネストを下げたほうが読みやすくなると思います。
| if (fewest_coins[i] != -1) { | ||
| 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 comment
The 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);だけで済むようになり、シンプルになると思います。
| int coinChange(vector<int>& coins, int amount) { | ||
| vector<int> min_coins(amount + 1, -1); | ||
| min_coins[0] = 0; | ||
| for (int i = 0; i <= amount; ++i) { |
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.
i よりは合計金額という意味がわかる変数名だとわかりやすいと思いました。
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.
本当はamountという変数名をつけたかったんですが、もともとの関数の引数の名前に取られてたのでiにしました。
ただ今考えたら勝手に変数名変えても別にleetcodeの実行上は支障なかったですね。
おっしゃる通りこちらのほうがわかりやすいと思います!
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();
}
};| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
枚数としてあり得ない大きい数で初期化したい派です。
問題文
https://leetcode.com/problems/coin-change/description/