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
13 changes: 13 additions & 0 deletions 0322-coin-change/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### step1

DFSでコイン1枚ずつ遷移させていく方法しか思いつかず、TLEしてしまったのでChatGPTにDPで解き直してもらった。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

TLEしたコードも参考に貼っておくと、良いと思います。

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.

そうですね。今回はもう消してしまってないので次回から参考に貼るようにします。

Copy link
Copy Markdown

@liquo-rice liquo-rice May 8, 2026

Choose a reason for hiding this comment

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

LeetCodeのSubmissionsタブから見れないですかね?

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.

ありがとうございます!見られました!こちらの機能知らなかったです。あとで追加しておきます。

他の方のPRを見ていると、配るDP, もらうDPに関していくつか記述があったので調べてみた。(https://algo-method.com/descriptions/78)
今回書いたDPは配るDPらしい。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

貰うDPの方でも書けますか?


### step2
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

他の人のコードを読んでみましたか?これ以外にも、再帰+memoizationの方法とかがあると思います。


変更点なし。

### step3

3回通すまで書き直し。
18 changes: 18 additions & 0 deletions 0322-coin-change/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
const int INF = 1e9;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1e9という数はどこから来ましたか?

INT_MAXを使わない理由はありますか?

Copy link
Copy Markdown
Owner Author

@hemispherium hemispherium May 7, 2026

Choose a reason for hiding this comment

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

なんとなく大きな数ということでこう書いてしまいました。INT_MAXやstd::numeric_limits<int>::max()を使うようにします。


vector<int> min_coins(amount + 1, INF);
min_coins[0] = 0;

for (int i = 0; i <= amount; i++) {
for (int coin : coins) {
if (i <= amount - coin) {
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 + coin <= 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.

i + coinがオーバーフローしてしまうことがあるためこう書いた気がします。

min_coins[i + coin] = min(min_coins[i + coin], min_coins[i] + 1);
}
}
}
return min_coins[amount] == INF ? -1 : min_coins[amount];
}
};
18 changes: 18 additions & 0 deletions 0322-coin-change/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
const int INF = 1e9;

vector<int> min_coins(amount + 1, INF);
min_coins[0] = 0;

for (int i = 0; i <= amount; i++) {
for (int coin : coins) {
if (i <= amount - coin) {
min_coins[i + coin] = min(min_coins[i + coin], min_coins[i] + 1);
}
}
}
return min_coins[amount] == INF ? -1 : min_coins[amount];
}
};
17 changes: 17 additions & 0 deletions 0322-coin-change/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
const int INF = 1e9;
vector<int> min_coins(amount + 1, INF);
min_coins[0] = 0;

for (int i = 0; i <= amount; i++) {
for (int coin : coins) {
if (i <= amount - coin) {
min_coins[i + coin] = min(min_coins[i + coin], min_coins[i] + 1);
}
}
}
return min_coins[amount] == INF ? -1 : min_coins[amount];
}
};