Skip to content

Conversation

@SuperHotDogCat
Copy link
Owner

@seal-azarashi
Copy link

Priority Queue を自前で実装することを oda さんが推奨されてました。
https://discord.com/channels/1084280443945353267/1195700948786491403/1199131558502735922

def add(self, val: int) -> int:
# Add new value and pop until len(self._min_heap) == self.k
heapq.heappush(self._min_heap, val)
while len(self._min_heap) > self.k:

Choose a reason for hiding this comment

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

ifでもよさそうかな?とおもいました!

Copy link
Owner Author

Choose a reason for hiding this comment

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

1個ずつしかでないので確かにそれでいいですね

@ryoooooory
Copy link

レビュー完了しました!

def add(self, val: int) -> int:
if len(self._min_heap) < self.k:
heappush(self._min_heap, val)
elif val >= self._min_heap[0]:
Copy link

Choose a reason for hiding this comment

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

Copy link
Owner Author

Choose a reason for hiding this comment

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

知らなかったです。ありがとうございます。

Comment on lines +10 to +12
if len(self._min_heap) < self.k:
heappush(self._min_heap, val)
elif val >= self._min_heap[0]:

Choose a reason for hiding this comment

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

if, elif で関心の対象は揃えた方が読みやすいと思います

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。ifとelifの使い分けが自分の中でイマイチできていませんでしたがこの説明が腑に落ちました。

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants