Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions 322/322.md
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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

範囲 for 文でプリミティブ型変数で回すときは、 const は付けないことが多いように思います。好みの問題かもしれません。

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) {
Copy link

Choose a reason for hiding this comment

The 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) {
Copy link

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);

だけで済むようになり、シンプルになると思います。

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

枚数としてあり得ない大きい数で初期化したい派です。

min_coins[0] = 0;
for (int i = 0; i <= amount; ++i) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i よりは合計金額という意味がわかる変数名だとわかりやすいと思いました。

Copy link
Owner Author

@potrue potrue Aug 9, 2025

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();
    }
};

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();
}
};
```