Open
Conversation
TrsmYsk
reviewed
Feb 20, 2026
| * そもそもk番目以降の要素を保存しておく必要はない | ||
| * 配列の末尾がk番目の要素になるようにする | ||
| * そしたら計算するときの配列の長さは最大でも10^4 | ||
| * システム全体では最悪の場合の計算量で、10^4 * (log(10^4) + 10^4)がかかる |
There was a problem hiding this comment.
計算量はある関数の極限における振る舞いの話なので、具体的な数値を代入した式を計算量と呼ぶことには違和感があります。このあたりの話をご覧ください。
liruly/leetcode#10 (comment)
Owner
Author
There was a problem hiding this comment.
ステップ数とするべきでしたね。ありがとうございます
ksaito0629
reviewed
Feb 20, 2026
| if k <= 0: | ||
| raise ValueError("k must be more than zero") | ||
| if len(nums) < k - 1: | ||
| raise ValueError("len(nums) must be more than ore equal to k - 1") |
There was a problem hiding this comment.
細かいですが、len(nums) + 1 < k の方が分かりやすいんじゃないですかね。add で追加するので。
| for num in nums: | ||
| heapq.heappush(self.topk_heap, num) | ||
| while len(self.topk_heap) > self.k: | ||
| heapq.heappop(self.topk_heap) |
There was a problem hiding this comment.
heapq library を一度見てみるとよいと思います。heapify 、nlargest, heappushpop, heapreplace 等々、他の選択肢も色々あります。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。
heappush, pop以外使ってなかったのでこれを機に調べてみます
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.
問題