Skip to content

322. coin change#30

Open
5ky7 wants to merge 2 commits into
mainfrom
322.-Coin-Change
Open

322. coin change#30
5ky7 wants to merge 2 commits into
mainfrom
322.-Coin-Change

Conversation

@5ky7

@5ky7 5ky7 commented Dec 25, 2025

Copy link
Copy Markdown
Owner

問題:322. Coin Change

@huyfififi huyfififi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

良いと思います 👍

Comment thread memo.md
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> min_num_coins(amount + 1, INT_MAX);
vector<int> is_constructable(amount + 1, 0); // 0: not constructable, 1: constructable

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私は std::vector<bool> として真偽値型を入れる方がわかりやすく感じます。is constructable? という質問に直接答えている感じがするからです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

std::vector は特殊化されるため、パフォーマンスが下がる場合がある点に注意が必要だと思います。個人的には std::vector のままで良いと思います。自分は std::vector を使うことが多いです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

std::vector<bool>の場合だけ特殊化されていて、内部のデータの扱いが異なるんですね。ご指摘いただきありがとうございます!

Comment thread memo.md
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> min_num_coins(amount + 1, INT_MAX);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: std::をつけているところとつけていないところがあり、統一されていないことが少しだけ気になりました。
実際どこのheaderにあるのか意識する、練習ではあるが業務でのコーディングとの差分を少なくする、ために

#include <vector>

class Solution {
public:
    int coinChange(std::vector<int>& coins, int amount) {

としても良いかもしれません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

leetcodeでは動くけども,実務を意識して敢えてincludeするということですね.見落としていた視点なので助かります.

Comment thread memo.md
}
for (int coin : coins) {
if (i + coin < min_num_coins.size()) {
min_num_coins[i + coin] = std::min(min_num_coins[i + coin], min_num_coins[i] + 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ただの感想なのですが

min_num_coins[i] = std::min(min_num_coins[i], min_num_coins[i - coin] + 1)

とする過去参照?の解法の方がよく見書ける気がします。
私は、例えばフィボナッチ数列の f(n) = f(n - 1) + f(n - 2) を脳内で考えると、5番目を計算するために4番目と3番目の結果を取ってきて...と後ろ向きに参照する方法が最初に思い浮かぶのですが、みなさんどうなのでしょうね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます!確かに,変数の使い方については私も気になるところですね.
自分の場合「必要になってから求める(再帰)」と「下から積み上げる(ループ)」という捉え方をしているのですが,今回だと,一旦メモ化再帰(必要になってから求める)で実装しようとして失敗したため,「では『下から積み上げる』で実装しよう」と思い積み上げていく変数の使い方をしているという可能性が高そうでした.

Comment thread memo.md
is_constructable[0] = 1;
min_num_coins[0] = 0;

for (long long i = 0; i < min_num_coins.size(); ++i) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

iの代わりに(amountとの名前被りを防ぎつつ)totalなどとした方が、なんの数字なのかわかりやすいかもしれません。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

同意です。「単なるループカウンタなのに long long が必要なの?」とびっくりしました。

Comment thread memo.md
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
int not_achievable = 10001;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的にはクラスに static constexpr な定数変数として定義したいです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます.
「どのインスタンスでも共通かつ変更しない定数」なのでstatic constかstatic constexprで定義したく,さらに配列のサイズなどにも用いる可能性(今後の拡張可能性)を考慮してstatic constexprで定義したい,という認識で間違いないでしょうか?

Comment thread memo.md
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> min_num_coins(amount + 1, INT_MAX);
vector<int> is_constructable(amount + 1, 0); // 0: not constructable, 1: constructable

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

std::vector は特殊化されるため、パフォーマンスが下がる場合がある点に注意が必要だと思います。個人的には std::vector のままで良いと思います。自分は std::vector を使うことが多いです。

Comment thread memo.md
```cpp
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

coinsは変更しないのでconst参照にしたほうがよいかなと思いました。

@nodchip nodchip mentioned this pull request Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants