-
Notifications
You must be signed in to change notification settings - Fork 0
322. Coin Change #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
322. Coin Change #38
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| ### step1 | ||
|
|
||
| DFSでコイン1枚ずつ遷移させていく方法しか思いつかず、TLEしてしまったのでChatGPTにDPで解き直してもらった。 | ||
| 他の方のPRを見ていると、配るDP, もらうDPに関していくつか記述があったので調べてみた。(https://algo-method.com/descriptions/78) | ||
| 今回書いたDPは配るDPらしい。 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 貰うDPの方でも書けますか? |
||
|
|
||
| ### step2 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 他の人のコードを読んでみましたか?これ以外にも、再帰+memoizationの方法とかがあると思います。 |
||
|
|
||
| 変更点なし。 | ||
|
|
||
| ### step3 | ||
|
|
||
| 3回通すまで書き直し。 | ||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1e9という数はどこから来ましたか? INT_MAXを使わない理由はありますか?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なんとなく大きな数ということでこう書いてしまいました。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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]; | ||
| } | ||
| }; | ||
| 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]; | ||
| } | ||
| }; |
| 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]; | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TLEしたコードも参考に貼っておくと、良いと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そうですね。今回はもう消してしまってないので次回から参考に貼るようにします。
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LeetCodeのSubmissionsタブから見れないですかね?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます!見られました!こちらの機能知らなかったです。あとで追加しておきます。