Open
Conversation
nanae772
reviewed
Sep 15, 2025
| k = 0 | ||
| nums = [] | ||
| def __init__(self, k: int, nums: List[int]): | ||
| self.k = k - 1 |
There was a problem hiding this comment.
k番目を取るには(k-1)番目を見なければいけないということで理解はできるのですが、それを理解できるのがaddのほうを読んだ後なので自分ならこちらは自然に見えるようself.k = kとして、addでreturnする際にself.k - 1とするかなと思いました。
| class KthLargest: | ||
| def __init__(self, k: int, nums: List[int]): | ||
| self.k = k | ||
| self.nums = [] |
There was a problem hiding this comment.
numsだけだと数値が入っていること以外が伝わりづらいため、上位k個のスコアを保持していることが分かるtop_k_scoresなどが良いかなと思いました
| self.k = k | ||
| self.nums = MinHeap() | ||
|
|
||
| for n in nums: |
There was a problem hiding this comment.
nはデータのサイズなどを表す際によく使われる一文字変数なので(O(n), O(nlogn)など計算量の表記などでもよく使われる)、個人的にはnumくらいにしたいなと思いました。
| self.heap[pos] = new_item | ||
| self._sift_up(pos) | ||
|
|
||
| def size(self): |
There was a problem hiding this comment.
これは__len__として実装してもよいかなと思いました(馴染みのあるlen()を使ってサイズが分かるようになるので)
https://docs.python.org/3/reference/datamodel.html#object.__len__
|
LGTM |
nodchip
reviewed
Sep 17, 2025
| ### 解答(AC) | ||
| ```py | ||
| class KthLargest: | ||
| k = 0 |
There was a problem hiding this comment.
クラス変数とインスタンス変数の両方を使っている点に違和感を感じました。インスタンス変数のみで十分だと思います。
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.
703. Kth Largest Element in a Stream
次回予告: 929. Unique Email Addresses