Skip to content
Open
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions 322/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*

Time Comprexity: O(n * m) (n: amount, m: coinsの長さ)
Space Complexity: O(n)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらをご確認ください

Hiroto-Iizuka/coding_practice#26 (comment)


intでオーバーフローしてしまい、脊髄反射的にlongを使用したがDPの方向を変えればオーバーフローしなさそう
*/
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<long> coin_nums(amount + 1, numeric_limits<long>::max());
coin_nums[0] = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

coin_numsは、コインの金額と解釈される可能性もありそうなので、num_coinsmin_num_coinsあたりがいいかなと思いました。

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.

コメントありがとうございます、なるほど、coinとしたほうが枚数に着目していることをより明示できそうですね。

for (long i = 0; i <= amount; ++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.

long はデータモデルによってビット数が変わります。
https://ja.wikipedia.org/wiki/64%E3%83%93%E3%83%83%E3%83%88#64%E3%83%93%E3%83%83%E3%83%88%E3%83%87%E3%83%BC%E3%82%BF%E3%83%A2%E3%83%87%E3%83%AB
原則避けたほうが無難だと思います。代わりに int64_t を使うことをお勧めいたします。

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.

コメントありがとうございます、longの性質、知りませんでした。参考にします 🙏

if (coin_nums[i] == numeric_limits<long>::max()) {
continue;
}
for (const long coin : coins) {
if (i + coin > amount) {
continue;
}
coin_nums[i + coin] = min(coin_nums[i] + 1, coin_nums[i + coin]);
}
}
if (coin_nums.back() == numeric_limits<long>::max()) {
return -1;
}
return coin_nums.back();
}
};
27 changes: 27 additions & 0 deletions 322/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
DPの方向を変えてオーバーフローしないようにした。
*/

class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> coin_nums(amount + 1, numeric_limits<int>::max());
coin_nums[0] = 0;
for (int to_coins = 1; to_coins <= amount; ++to_coins) {

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.

coinsだと、合計値を指すのか枚数を指すのかが不明瞭になるので別の名前を使うべきだった

for (const int coin : coins) {
int from_coins = to_coins - coin;
if (from_coins < 0) {
continue;
}
if (coin_nums[from_coins] == numeric_limits<int>::max()) {
continue;
}
coin_nums[to_coins] = min(coin_nums[from_coins] + 1, coin_nums[to_coins]);
}
}
if (coin_nums.back() == numeric_limits<int>::max()) {
return -1;
}
return coin_nums.back();
}
};
23 changes: 23 additions & 0 deletions 322/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> min_coins(amount + 1, numeric_limits<int>::max());
min_coins[0] = 0;
for (int to_price = 1; to_price <= amount; ++to_price) {
for (const int coin : coins) {
int from_price = to_price - coin;
if (from_price < 0) {
continue;
}
if (min_coins[from_price] == numeric_limits<int>::max()) {
continue;
}
min_coins[to_price] = min(min_coins[from_price] + 1, min_coins[to_price]);
}
}
if (min_coins.back() == numeric_limits<int>::max()) {
return -1;
}
return min_coins.back();
}
};