Conversation
| class KthLargest: | ||
| def __init__(self, k: int, nums: List[int]): | ||
| sorted_nums = sorted(nums, reverse=True) | ||
| self.top_k_min_heap = sorted_nums[:k] |
There was a problem hiding this comment.
細かい点となりますが、公開されていないメソッド名やインスタンス変数名には、先頭に _ を付けることをお勧めします。
There was a problem hiding this comment.
ありがとうございます!メソッド名だけ気にしてメンバの変数名については失念していました.
|
|
||
| - https://discord.com/channels/1084280443945353267/1195700948786491403/1198906989070258277 | ||
| - "priority queue は一回実装しておいてください。" → 実装します. | ||
| - priority queue と heap の違いって何と思って調べたら, priority queue は abstract data type であり, heap はその実装の一つだということだった. |
There was a problem hiding this comment.
勉強になります。PythonだとBinary Heapで実装されていますが、Priority Queueの実装としては他にも色々あるんですねー。
There was a problem hiding this comment.
こちらこそいつも勉強になっています!ヒープですとここに様々なものがまとめられてますね!
binary heapよりも計算量が良いものが色々あるみたいです
| - priority queue ある. | ||
| - [参考 priority queue 実装](https://discord.com/channels/1084280443945353267/1192736784354918470/1194613857046503444) | ||
|
|
||
| heap を実装してみるが, エラー処理やどこまでメソッド化するかが難しい. |
There was a problem hiding this comment.
CPythonの実装を参考にしてみると良いと思います。
https://github.com/python/cpython/blob/main/Lib/heapq.py
popやpushの実装が大きくなっていますが関数に切り出せそうです。切り出すとheapifyや初期化時に配列を受け取るケースとかを追加で実装しようとする際に比較的楽になると思います。
There was a problem hiding this comment.
ありがとうございます!
なるほど, shift up/downとして切り出せるのですね
There was a problem hiding this comment.
正確にはsift up/downですね。
shift up/downでもそんなに意味が変わらない気がするので紛らわしいですね。
There was a problem hiding this comment.
本当ですね, siftという単語を今まで見なかったので気付きませんでした. ありがとうございます
Problem link