322. Coin Change#51
Open
skypenguins wants to merge 1 commit into
Open
Conversation
colorbox
reviewed
Jul 23, 2026
|
|
||
| for current_amount in range(1, amount + 1): | ||
| for coin in coins: | ||
| if current_amount >= coin: |
There was a problem hiding this comment.
ここの条件を反転し、continueでコードのネストを減らす方が読みやすいと思います。
nodchip
reviewed
Jul 23, 2026
| ```py | ||
| class Solution: | ||
| def coinChange(self, coins: List[int], amount: int) -> int: | ||
| min_num_coins = [float("inf")] * (amount + 1) |
There was a problem hiding this comment.
min_ という接頭辞は、しばしば minimum number of の略とされると思います。 min_coins で十分伝わると思います。
| if amount == 0: | ||
| return 0 | ||
| current_amounts = [0] | ||
| found = set() |
There was a problem hiding this comment.
set より list のほうがデータ構造が単純で、処理が軽いイメージがあるため、個人的には list を使うことが多いです。
found = [False] * amountただ、定数倍の速度差しかありません。定数倍の速度差を気にするのであれば、 C++ 等、高速な言語で書いたほうが良いとも思います。
| for current_amount in range(1, amount + 1): | ||
| for coin in coins: | ||
| if current_amount >= coin: | ||
| min_num_coins[current_amount] = min(min_num_coins[current_amount], min_num_coins[current_amount - coin] + 1,) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
322. Coin Change
次回予告: 323. Number of Connected Components in an Undirected Graph