Open
Conversation
nodchip
reviewed
Feb 23, 2026
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
| if k <= 0: | ||
| raise ValueError("k must be more than 0.") | ||
| value_to_count = {} |
There was a problem hiding this comment.
collections.defaultdict を使うと、もう少しシンプルに書けると思います。
https://docs.python.org/ja/3.14/library/collections.html#collections.defaultdict
TrsmYsk
reviewed
Mar 2, 2026
| value_to_count = {} | ||
| for num in nums: | ||
| value_to_count[num] = value_to_count.get(num, 0) + 1 | ||
| max_heap = [] |
There was a problem hiding this comment.
heapqモジュールはmax heap用の関数群も用意してくれているので、それらを使っていないのにmax_heapという名づけをすると読み手が混乱するような気がします。
https://docs.python.org/3.14/library/heapq.html
振り返りコメントにもありましたが、要素数をkに抑えて、top_kみたいに名付けるのが一番素直かなと思います。
TrsmYsk
reviewed
Mar 2, 2026
| pivot = num_to_counts[unique_nums[right]] | ||
| partition_idx = left | ||
| for i in range(left, right): | ||
| if num_to_counts[unique_nums[i]] <= pivot: |
There was a problem hiding this comment.
条件分岐をif num_to_counts[unique_nums[i]] > pivot:にしてcotinueすることで、ll.276-277のインデントを上げてもいいのかなと思いました。たぶん趣味の範囲です。
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.
問題
https://leetcode.com/problems/top-k-frequent-elements/description/