Skip to content

322. Coin Change#38

Open
5103246 wants to merge 1 commit intomainfrom
322-coin-change
Open

322. Coin Change#38
5103246 wants to merge 1 commit intomainfrom
322-coin-change

Conversation

@5103246
Copy link
Owner

@5103246 5103246 commented Jan 2, 2026

- n : coins.length, m : amount
- 時間計算量:O(n * m)
- 空間計算量:O(m)
- 実行時間は、12 * 10 ^ 4 / 10 ^ 8 = 1.2 ms程度
Copy link

Choose a reason for hiding this comment

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

10 ^ 8 がどこから出てきた数字なのかが気になりました。見落としていたらすみません。

Choose a reason for hiding this comment

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

おそらく、CPUが1秒にC++の何ステップ分計算できるかの値だと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

そうですね。
それについてのリンクも貼っておきます。
Yuto729/LeetCode_arai60#16 (comment)

CPU の周波数が数 GHz 程度であるため、IPC(Instructions per Cycle)を 1 と仮定すると、1 秒間に数十億命令を実行できます。
C++ はオーバーヘッドが小さく、1 ステップを数命令〜数十命令で実行できるため、おおよそ 1~10 億ステップ/秒が目安となります。

Copy link

@naoto-iwase naoto-iwase left a comment

Choose a reason for hiding this comment

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

全体的に読みやすいと感じました。

- n : coins.length, m : amount
- 時間計算量:O(n * m)
- 空間計算量:O(m)
- 実行時間は、12 * 10 ^ 4 / 10 ^ 8 = 1.2 ms程度

Choose a reason for hiding this comment

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

おそらく、CPUが1秒にC++の何ステップ分計算できるかの値だと思います。

return -1;
}

vector<int> min_coins(amount + 1, numeric_limits<int>::max());

Choose a reason for hiding this comment

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

min_coinsだと引数のcoinsも相まって最小金額のコインという感じが若干する気がするので、min_num_coinsとすると枚数の最小値であることが明確になるかもしれません。

coin_and_sum.push({0, 0});

while (!coin_and_sum.empty()) {
auto [num_coin, sum] = coin_and_sum.top();

Choose a reason for hiding this comment

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

nit: num_coinですが、number of coinsの略として、num_coin"s"としたい気がします。

if (coin > i) {
continue;
}
if (min_coins[i - coin] == numeric_limits<int>::max()) {
Copy link

Choose a reason for hiding this comment

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

vector<int> min_coins(amount + 1, numeric_limits<int>::max() / 2);

と初期化すると、 min_coins[i - coin] + 1 でオーバーフローしなくなるため、この if 文が不要となります。ただ、意図が分かりづらいため、現状のままのほうが良いかもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます。
たしかに、その書き方ならオーバーフローの心配もいらないですね。
参考になります。

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.

4 participants