Skip to content

Wanwan87 703. kth largest element in a stream#8

Open
wanwan87 wants to merge 2 commits into
mainfrom
wanwan87-703.-Kth-Largest-Element-in-a-Stream
Open

Wanwan87 703. kth largest element in a stream#8
wanwan87 wants to merge 2 commits into
mainfrom
wanwan87-703.-Kth-Largest-Element-in-a-Stream

Conversation

@wanwan87
Copy link
Copy Markdown
Owner

@wanwan87 wanwan87 commented Jun 2, 2026

self.top_k = nums[:k] #先頭k個を取り出す
heapq.heapify(self.top_k) #最小ヒープに変換
for i in range(k, len(nums)):
heapq.heappushpop(self.top_k,nums[i]) #nums[i]の値をpushして最小値をpopする
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

, のあとにスペースが空いているものと空いていないものが混在しているのが気になりました。空けるほうに統一するとよいと思います。

参考までにスタイルガイドへのリンクを共有いたします。

https://google.github.io/styleguide/pyguide.html#36-whitespace

Do use whitespace after a comma, semicolon, or colon, except at the end of the line.

なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます、意識します

- 書いてみてエラーがいくつか出たので修正
- コンストラクタ内でself.変数=値とすることで、値とインスタンスを紐づけて格納する。selfはインスタンス自身を指すので、インスタンスが複数あっても、値を別々に保持できる。
- sortとsortedでソートできる。sortは元のリスト自身を並び替える、sorted(リスト)は元を変えずに新しい並び替えリストを返す。sortedだと変数が増えるのでsortを選択。
-
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

コードを書く前に実行時間を見積もるとよいと思います。
こちらをご参照ください
Yuto729/leetcode#16 (comment)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます、意識してなかったので今後考えてみます。

ソート解 O(Mnlon(n))
ヒープ解 O(M*logk)
M=10^4
n=10^4+10^4

遅い場合:10^6 [ステップ/s]
早い場合:10^7

ソート
遅い側:(10^4 * 210^4 * 14.3) /10^6 = 28.610^2
早い側:286秒

ヒープ
遅い側:(10^4 * 14.3) /10^6 = 14.310^-2
早い側:14.3
10^-3

結構時間変わるんですね

self.top_k = nums[:k]
heapq.heapify(self.top_k)
for i in range(k, len(nums)):
heapq.heappushpop(self.top_k,nums[i])
Copy link
Copy Markdown

@h-masder h-masder Jun 3, 2026

Choose a reason for hiding this comment

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

numsのk個目までとそれ以降をわけずに処理をしてもいいのかなと思いました。

例えば、add関数を使う方法があります。

    def __init__(self, k: int, nums: List[int]):
        self.k = k
        self.top_k = []

        for num in nums:
            self.add(num)

Copy link
Copy Markdown
Owner Author

@wanwan87 wanwan87 Jun 3, 2026

Choose a reason for hiding this comment

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

ありがとうございます、提案頂いたもののほうが読みやすい気がしますね

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