Open
Conversation
oda
reviewed
Jan 22, 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 |
There was a problem hiding this comment.
k 個取っておくようにすれば間に合いましたね。sorted array は意外と速いです。insort も参考にどうぞ。
https://docs.python.org/3/library/bisect.html#bisect.insort_right
Owner
Author
There was a problem hiding this comment.
sortの順番を崩さずに挿入する関数があるんですね、頭に入れておきます。
oda
reviewed
Jan 22, 2025
| ### コメント | ||
|
|
||
| - この書き方だと’add’という関数名が微妙に実態からずれて気もする(k番目の値より小さい要素を削ったりしているので)けど、問題文でaddと指定されているのでよしとしよう | ||
| - 変数名をtop_k_valueにしただけで今何をしているのかわかりやすくなり、コードが格段に頭に入りやすくなる現象を確認しておもしろい |
There was a problem hiding this comment.
変数名をtop_k_valueにしただけで今何をしているのかわかりやすくなり、コードが格段に頭に入りやすくなる現象を確認しておもしろい
これ大事ですね。
t0hsumi
reviewed
Jan 22, 2025
| class KthLargest: | ||
| def __init__(self, k: int, nums: List[int]): | ||
| self.k = k | ||
| self.top_k_value = [] |
Owner
Author
There was a problem hiding this comment.
確かにそうですね。前もご指摘いただいたのでもう少し気を使います。
fuga-98
reviewed
Feb 22, 2025
| ## Step 3 | ||
| ### コメント | ||
|
|
||
| - この書き方だと’add’という関数名が微妙に実態からずれて気もする(k番目の値より小さい要素を削ったりしているので)けど、問題文でaddと指定されているのでよしとしよう |
There was a problem hiding this comment.
言われてみればそうですね
実際に使うのであれば破壊的処理をしているのでNoneを返すほうが親切ですね
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題文:https://leetcode.com/problems/kth-largest-element-in-a-stream/description/
次に解く:https://leetcode.com/problems/top-k-frequent-elements/description/