-
Notifications
You must be signed in to change notification settings - Fork 0
703. Kth Largest Element in a Stream #23
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
| k = 0 | ||
| nums = [] | ||
| def __init__(self, k: int, nums: List[int]): | ||
| self.k = k - 1 |
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番目を取るには(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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
numsだけだと数値が入っていること以外が伝わりづらいため、上位k個のスコアを保持していることが分かるtop_k_scoresなどが良いかなと思いました
| self.k = k | ||
| self.nums = MinHeap() | ||
|
|
||
| for n in nums: |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これは__len__として実装してもよいかなと思いました(馴染みのあるlen()を使ってサイズが分かるようになるので)
https://docs.python.org/3/reference/datamodel.html#object.__len__
|
LGTM |
| ### 解答(AC) | ||
| ```py | ||
| class KthLargest: | ||
| k = 0 |
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.
クラス変数とインスタンス変数の両方を使っている点に違和感を感じました。インスタンス変数のみで十分だと思います。
703. Kth Largest Element in a Stream
次回予告: 929. Unique Email Addresses