Skip to content

Conversation

@skypenguins
Copy link
Owner

703. Kth Largest Element in a Stream

次回予告: 929. Unique Email Addresses

@skypenguins skypenguins self-assigned this Sep 15, 2025
k = 0
nums = []
def __init__(self, k: int, nums: List[int]):
self.k = k - 1

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 = []

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:

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):

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__

@5103246
Copy link

5103246 commented Sep 16, 2025

LGTM

### 解答(AC)
```py
class KthLargest:
k = 0
Copy link

Choose a reason for hiding this comment

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

クラス変数とインスタンス変数の両方を使っている点に違和感を感じました。インスタンス変数のみで十分だと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants