Skip to content

703-kth-largest-element-in-a-stream#8

Open
kitano-kazuki wants to merge 6 commits intomainfrom
703-kth-largest-element-in-a-stream
Open

703-kth-largest-element-in-a-stream#8
kitano-kazuki wants to merge 6 commits intomainfrom
703-kth-largest-element-in-a-stream

Conversation

@kitano-kazuki
Copy link
Owner

@kitano-kazuki kitano-kazuki commented Feb 20, 2026

* そもそもk番目以降の要素を保存しておく必要はない
* 配列の末尾がk番目の要素になるようにする
* そしたら計算するときの配列の長さは最大でも10^4
* システム全体では最悪の場合の計算量で、10^4 * (log(10^4) + 10^4)がかかる
Copy link

Choose a reason for hiding this comment

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

計算量はある関数の極限における振る舞いの話なので、具体的な数値を代入した式を計算量と呼ぶことには違和感があります。このあたりの話をご覧ください。
liruly/leetcode#10 (comment)

Copy link
Owner Author

Choose a reason for hiding this comment

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

ステップ数とするべきでしたね。ありがとうございます

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")

Choose a reason for hiding this comment

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

細かいですが、len(nums) + 1 < k の方が分かりやすいんじゃないですかね。add で追加するので。

Copy link
Owner Author

Choose a reason for hiding this comment

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

そっちの方がわかりやすそうですね!

for num in nums:
heapq.heappush(self.topk_heap, num)
while len(self.topk_heap) > self.k:
heapq.heappop(self.topk_heap)

Choose a reason for hiding this comment

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

heapq library を一度見てみるとよいと思います。heapify 、nlargest, heappushpop, heapreplace 等々、他の選択肢も色々あります。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
heappush, pop以外使ってなかったのでこれを機に調べてみます

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants