Skip to content

Create kth_largest_element_in_a_straem.md#10

Open
SanakoMeine wants to merge 3 commits intomainfrom
kth_largest_element_in_a_stream
Open

Create kth_largest_element_in_a_straem.md#10
SanakoMeine wants to merge 3 commits intomainfrom
kth_largest_element_in_a_stream

Conversation

@SanakoMeine
Copy link
Owner

@SanakoMeine SanakoMeine commented Jan 21, 2025

- 読んだことないならheapの公式ドキュメントは読んでおこうとのこと(nlargestとかあるな):https://docs.python.org/3/library/heapq.html#heapq.heappushpop
- Heapifyする代わりにaddで回すのは読みやすくて行数も短いので確かに良さそう。
- top_k_heapはわかりやすいので頂戴しよう
- Heapを知らない自分でも解ける方法はあったのか…:https://github.com/rinost081/LeetCode/pull/9/files のstep1
Copy link

Choose a reason for hiding this comment

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

k 個取っておくようにすれば間に合いましたね。sorted array は意外と速いです。insort も参考にどうぞ。
https://docs.python.org/3/library/bisect.html#bisect.insort_right

Copy link
Owner Author

Choose a reason for hiding this comment

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

sortの順番を崩さずに挿入する関数があるんですね、頭に入れておきます。

### コメント

- この書き方だと’add’という関数名が微妙に実態からずれて気もする(k番目の値より小さい要素を削ったりしているので)けど、問題文でaddと指定されているのでよしとしよう
- 変数名をtop_k_valueにしただけで今何をしているのかわかりやすくなり、コードが格段に頭に入りやすくなる現象を確認しておもしろい
Copy link

Choose a reason for hiding this comment

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

変数名をtop_k_valueにしただけで今何をしているのかわかりやすくなり、コードが格段に頭に入りやすくなる現象を確認しておもしろい

これ大事ですね。

Copy link

@t0hsumi t0hsumi left a comment

Choose a reason for hiding this comment

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

いいと思います

class KthLargest:
def __init__(self, k: int, nums: List[int]):
self.k = k
self.top_k_value = []
Copy link

Choose a reason for hiding this comment

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

細かいですが、top_k_valuesの方が適切だと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにそうですね。前もご指摘いただいたのでもう少し気を使います。

## Step 3
### コメント

- この書き方だと’add’という関数名が微妙に実態からずれて気もする(k番目の値より小さい要素を削ったりしているので)けど、問題文でaddと指定されているのでよしとしよう
Copy link

Choose a reason for hiding this comment

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

言われてみればそうですね
実際に使うのであれば破壊的処理をしているのでNoneを返すほうが親切ですね

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.

4 participants