-
Notifications
You must be signed in to change notification settings - Fork 0
Kth largest element in a stream #38
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: arai60
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # 普通に苦戦した | ||
| # 思考録: Quick selectアルゴリズムを思いつく, だが動的に配列が変化するので無駄に計算が増え, 他に効率が良いものがないか考える | ||
| # じゃあdequeでmax_len=kを指定してやれば良いかなと思ったが, これだと結局途中に要素を挿入するときに時間計算量がO(k)程度かかるので断念 | ||
| # 要素数をkに保ったmin_heapのrootを出力する方向で制作, これなら挿入操作もO(logk)程度で良い | ||
| # heapqを覚えていなかったのでhttps://docs.python.org/ja/3/library/heapq.htmlを確認した。 | ||
| # heapq.heapreplaceかheapq.heappushpopのどちらを使うか迷う。前者はpopしてからpush, 後者はpushしてからpopでどちらも要素数は保存されるのでどちらでも良いかなと思ったが... | ||
| class KthLargest: | ||
|
|
||
| def __init__(self, k: int, nums: List[int]): | ||
| self.min_heap = [] # min_heap has k elements and min_heap[0] means the k-th largest element in the stream | ||
| self.k = k | ||
| for num in nums: | ||
| self.add(num) | ||
|
|
||
| def add(self, val: int) -> int: | ||
| if len(self.min_heap) < self.k: | ||
| heapq.heappush(self.min_heap, val) | ||
| elif val > self.min_heap[0]: | ||
| heapq.heappushpop(self.min_heap, val) | ||
|
|
||
| return self.min_heap[0] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """ | ||
| Reference: | ||
| ryoooooory: https://github.com/ryoooooory/LeetCode/pull/15/files | ||
| seal-azarashi: https://github.com/seal-azarashi/leetcode/pull/8#pullrequestreview-2153924472 | ||
| Yoshiki-Iwasa: https://github.com/Yoshiki-Iwasa/Arai60/pull/7 | ||
| kazukiii: https://github.com/kazukiii/leetcode/pull/9/files | ||
| hayashi-ay: https://github.com/hayashi-ay/leetcode/pull/54/files | ||
| cheeseNA: https://github.com/cheeseNA/leetcode/pull/12/files | ||
|
|
||
| add method以外でmin_heap propertyをいじって欲しくないのでアンダーバーをつけてprivateであることを主張して書くべきだと感じた | ||
| ryooooooryさんのコメントにあった: 順次追加処理かつソート操作に強いPriorityQueueで実装とあったので少し頭の片隅にいれておく | ||
|
|
||
| """ | ||
| class KthLargest: | ||
|
|
||
| def __init__(self, k: int, nums: List[int]): | ||
| self._min_heap = [] # _min_heap has k elements and _min_heap[0] means the k-th largest element in the stream | ||
| self.k = k | ||
| for num in nums: | ||
| self.add(num) | ||
|
|
||
| def add(self, val: int) -> int: | ||
| if len(self._min_heap) < self.k: | ||
| heapq.heappush(self._min_heap, val) | ||
| elif val > self._min_heap[0]: | ||
| heapq.heappushpop(self._min_heap, val) | ||
|
|
||
| return self._min_heap[0] | ||
|
|
||
| # 別のheapq実装もためす。https://docs.python.org/ja/3/library/heapq.htmlにはheappushpopを使った方が1回ずつpushとpopを呼び出すよりも効率が良いので, 上の方が良い? | ||
| class KthLargest: | ||
|
|
||
| def __init__(self, k: int, nums: List[int]): | ||
| self._min_heap = [] # _min_heap has k elements and _min_heap[0] means the k-th largest element in the stream | ||
| self.k = k | ||
| for num in nums: | ||
| self.add(num) | ||
|
|
||
| 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: | ||
| heapq.heappop(self._min_heap) | ||
|
|
||
| return self._min_heap[0] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class KthLargest: | ||
|
|
||
| def __init__(self, k: int, nums: List[int]): | ||
| self._min_heap = [] # _min_heap has k elements and _min_heap[0] means the k-th largest element in the stream. | ||
| self.k = k | ||
| for num in nums: | ||
| self.add(num) | ||
|
|
||
| def add(self, val: int) -> int: | ||
| if len(self._min_heap) < self.k: | ||
| heappush(self._min_heap, val) | ||
| elif val >= self._min_heap[0]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/python/cpython/blob/b765e4adf858ff8a8646f38933a5a355b6d72760/Lib/heapq.py#L163
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 知らなかったです。ありがとうございます。
Comment on lines
+10
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if, elif で関心の対象は揃えた方が読みやすいと思います
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。ifとelifの使い分けが自分の中でイマイチできていませんでしたがこの説明が腑に落ちました。 |
||
| heappushpop(self._min_heap, val) | ||
|
|
||
| return self._min_heap[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.
ifでもよさそうかな?とおもいました!
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.
1個ずつしかでないので確かにそれでいいですね