Conversation
| - n : coins.length, m : amount | ||
| - 時間計算量:O(n * m) | ||
| - 空間計算量:O(m) | ||
| - 実行時間は、12 * 10 ^ 4 / 10 ^ 8 = 1.2 ms程度 |
There was a problem hiding this comment.
10 ^ 8 がどこから出てきた数字なのかが気になりました。見落としていたらすみません。
There was a problem hiding this comment.
おそらく、CPUが1秒にC++の何ステップ分計算できるかの値だと思います。
There was a problem hiding this comment.
そうですね。
それについてのリンクも貼っておきます。
Yuto729/LeetCode_arai60#16 (comment)
CPU の周波数が数 GHz 程度であるため、IPC(Instructions per Cycle)を 1 と仮定すると、1 秒間に数十億命令を実行できます。
C++ はオーバーヘッドが小さく、1 ステップを数命令〜数十命令で実行できるため、おおよそ 1~10 億ステップ/秒が目安となります。
| - n : coins.length, m : amount | ||
| - 時間計算量:O(n * m) | ||
| - 空間計算量:O(m) | ||
| - 実行時間は、12 * 10 ^ 4 / 10 ^ 8 = 1.2 ms程度 |
There was a problem hiding this comment.
おそらく、CPUが1秒にC++の何ステップ分計算できるかの値だと思います。
| return -1; | ||
| } | ||
|
|
||
| vector<int> min_coins(amount + 1, numeric_limits<int>::max()); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
nit: num_coinですが、number of coinsの略として、num_coin"s"としたい気がします。
| if (coin > i) { | ||
| continue; | ||
| } | ||
| if (min_coins[i - coin] == numeric_limits<int>::max()) { |
There was a problem hiding this comment.
vector<int> min_coins(amount + 1, numeric_limits<int>::max() / 2);と初期化すると、 min_coins[i - coin] + 1 でオーバーフローしなくなるため、この if 文が不要となります。ただ、意図が分かりづらいため、現状のままのほうが良いかもしれません。
There was a problem hiding this comment.
コメントありがとうございます。
たしかに、その書き方ならオーバーフローの心配もいらないですね。
参考になります。
今回の問題
https://leetcode.com/problems/coin-change/
次の問題
https://leetcode.com/problems/search-insert-position/