-
Notifications
You must be signed in to change notification settings - Fork 0
Create kth_largest_element_in_a_straem.md #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- 読んだことないなら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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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にしただけで今何をしているのかわかりやすくなり、コードが格段に頭に入りやすくなる現象を確認しておもしろい |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
変数名をtop_k_valueにしただけで今何をしているのかわかりやすくなり、コードが格段に頭に入りやすくなる現象を確認しておもしろい
これ大事ですね。
There was a problem hiding this 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 = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
細かいですが、top_k_valuesの方が適切だと思います。
There was a problem hiding this comment.
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と指定されているのでよしとしよう |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
言われてみればそうですね
実際に使うのであれば破壊的処理をしているのでNoneを返すほうが親切ですね
問題文:https://leetcode.com/problems/kth-largest-element-in-a-stream/description/
次に解く:https://leetcode.com/problems/top-k-frequent-elements/description/