Skip to content

703. Kth Largest Element in a Stream - #8

Open
kazizi55 wants to merge 2 commits into
mainfrom
703-kth-largest-element-in-a-stream
Open

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

Conversation

@kazizi55

@kazizi55 kazizi55 commented Nov 5, 2025

Copy link
Copy Markdown
Owner

@kazizi55
kazizi55 marked this pull request as ready for review November 5, 2025 02:40
- Timsort とはマージソートと挿入ソートを組み合わせたハイブリッドで安定なソートアルゴリズム
- https://en.wikipedia.org/wiki/Timsort

> `add()` を呼び出すたびに毎回ソートし、 `nums` のリストを生成しているため、 $10^4$ 回呼び出すと実行時間 2000ms 超となっていると想像

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

時間計算量から、どのくらいの処理時間がかかるか推定することをおすすめします。推定の方法は過去のコードレビューコメントにあると思いますので、探すことをおすすめします。

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.

ありがとうございます。
確かに時間がどれくらいかかるかまで推定できた方が、実際プログラムが動く時のことやどこまでスケールさせられるかなども考えられて便利ですね!

クロック数を1GHzと仮定した場合に、最大実行時間としてはこの解法だと約14秒と推定することができました。

  • 時間計算量が $O(m × n \log n)$ なので、問題の制約に基づき、m = n = 10^4とした場合に、
    $$10^8 × \log_2 10^4$$ になり、1.4 * 10^9 オペレーションになる。
  • 1GHzの場合、1オペレーションはおよそ10nsかかる (10クロックサイクル)
  • 1秒間 (10^9 ns)だと、10^9 ns / 10ns = 10^8オペレーション行うことができる
  • 1.4 * 10^9 / 10^8 = 14秒かかる

参考:

Comment on lines +7 to +8
self.k = k
self.top_k_nums = []

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分なら、self.k -> self.capacity、self.top_k_nums -> self.largest_numsと命名すると思います。

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.

ありがとうございます。
問題文に寄せる形にしてみていた (k を意識的に使うようにした)のですが、問題文に関係なく、ご提示いただいた変数名の方が中身を自然に想像することができる気がして良いですね!
その変数名で step 4 として解き直してみました。
https://github.com/kazizi55/coding-challenges/pull/8/files#diff-164241027a674ce8299ec45934ed8147699b5494cc9e9a2027eeb8d142b1251eR19-R36

@@ -0,0 +1,17 @@
from heapq import heappush, heappop

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分が以前いただいたコメントより:

どのスタイルに準拠するかによりますが、たとえば Google では import itertools -> itertools.islice といった使い方をします。
https://google.github.io/styleguide/pyguide.html#22-imports
LeetCode で簡便のためこのようにインポートすることは問題ないと思いますが、スタイルによってバリエーションがあることは頭にあると良いかと思います。

https://discord.com/channels/1084280443945353267/1421920158506549470/1432896430477017110

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.

ありがとうございます!
知らなかったので勉強になりました。
一方で pep8 ではそのような決まりは特になかったので、やはりスタイルによってバリエーションがあるのですね。
https://peps.python.org/pep-0008/#imports

package や module 単位で import するように step 4で書き直してみました!
https://github.com/kazizi55/coding-challenges/pull/8/files#diff-164241027a674ce8299ec45934ed8147699b5494cc9e9a2027eeb8d142b1251eR19-R36

Comment on lines +9 to +10
if len(self.top_k_nums) > self.k:
self.top_k_nums.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.

そもそもネストはそこまで深くないので好みですが、条件を反転してネストを浅くできるところは浅くするかなと思いました。

Suggested change
if len(self.top_k_nums) > self.k:
self.top_k_nums.pop()
if len(self.top_k_nums) <= self.k:
continue
self.top_k_nums.pop()

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.

ありがとうございます!
確かにネストを浅くできるところは極力浅くしておいた方が読み手側の負荷が下がりそうでいいですね。
https://github.com/kazizi55/coding-challenges/pull/8/files#diff-164241027a674ce8299ec45934ed8147699b5494cc9e9a2027eeb8d142b1251eR9-R11

class KthLargest:

def __init__(self, k: int, nums: List[int]):
self.list = sorted(nums, reverse=True)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

listであることは明白ですので自分ならsorted_numsなどの変数名にしたいと思いました。

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.

5 participants