-
Notifications
You must be signed in to change notification settings - Fork 0
Solved: 703. Kth Largest Element in a Stream #8
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?
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,71 @@ | ||
| ## 取り組み方 | ||
| - step1: 5分以内に空で書いてAcceptedされるまで解く | ||
| - step2: discordとwebで検索した結果を踏まえ、コードの洗練を洗練させる + 何を悪いと思ったか、何を避けたいと思ったか等感じたことを記載する | ||
| - step3: 10分以内に1回もエラーを出さずに3回連続で解く | ||
| - step4: binary heapを実装して解く | ||
|
|
||
| ## step1 | ||
| ```python | ||
| class KthLargest: | ||
| def __init__(self, k: int, nums: List[int]): | ||
| self.k = k | ||
| self.from_top_to_k = nums | ||
| heapq.heapify(self.from_top_to_k) | ||
|
|
||
| def add(self, val: int) -> int: | ||
| heapq.heappush(self.from_top_to_k, val) | ||
| while len(self.from_top_to_k) > self.k: | ||
| heapq.heappop(self.from_top_to_k) | ||
| return self.from_top_to_k[0] | ||
| ``` | ||
| ### 思考過程 | ||
| step1: heapqを用いてスコアのをヒープを管理する | ||
| step2: 要素の数をk個まで減らせば最小ヒープ = k番目に大きい数になる | ||
|
|
||
| - 値を負にして格納する方針もあるが、ミスりそう | ||
|
|
||
| N を nums.length として、 | ||
| 時間計算量は | ||
| - valの挿入: O(logN) | ||
| - nums.length - k 回の削除: O(NlogN) | ||
| - 最小ヒープの取得: O(1) | ||
| となるので、O(NlogN)。 | ||
| 空間計算量はヒープの使用でO(N)。 | ||
|
|
||
| ### 感想 | ||
| - numsのデータを削除しないで取っておいて、別のヒープを作った方がスコアの一覧をみたいとなった場合も考慮できて丁寧なのかもしれない | ||
|
|
||
| ## step2 | ||
| ```python | ||
| class KthLargest: | ||
| def __init__(self, k: int, nums: List[int]): | ||
| self.k = k | ||
| self.top_k = nums | ||
| heapq.heapify(self.top_k) | ||
|
|
||
| def add(self, val: int) -> int: | ||
| heapq.heappush(self.top_k, val) | ||
| while len(self.top_k) > self.k: | ||
| heapq.heappop(self.top_k) | ||
| return self.top_k[0] | ||
| ``` | ||
| ### 感想 | ||
| - たまたまnumsが空で来た時に対処できるコードになっていただけで、step1でnumsが空のケースを意識できていなかったことに反省した | ||
|
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. 最近はじまったオープンソースクイズですが、こういったところのイシューをみてもらうと、最終的に、これくらいの精度でコードが読解されるのだということが分かるかと思います。 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. 他の人のコードを読んで「感想を持つ」というところをして欲しいですね。大事なのはそこなので。 「この程度のものを書けることはどうでもいい」ということを理解するというのがこの練習の目標です。
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. レビューありがとうございます。
簡単なクイズに参加して、感覚掴もうと思います。ありがとうございます。 |
||
| - heappushをheappushpopに使用としたことで気づいた | ||
|
|
||
| ## step3 | ||
| ```python | ||
| class KthLargest: | ||
| def __init__(self, k: int, nums: List[int]): | ||
| self.k = k | ||
| self.top_k = nums | ||
| heapq.heapify(self.top_k) | ||
|
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. heapqは省略したほうが見やすいかもしれません。 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. 普通の環境であれば下記のように個別の関数をインポートしておくと、 みたいに先頭の heapq. を省略できるということですね。(他の関数も同様) |
||
|
|
||
| def add(self, val: int) -> int: | ||
| heapq.heappush(self.top_k, val) | ||
| while len(self.top_k) > self.k: | ||
| heapq.heappop(self.top_k) | ||
|
Comment on lines
+65
to
+67
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.
|
||
| return self.top_k[0] | ||
| ``` | ||
| ### 感想 | ||
| - すんなり解けたので、次は binary heap を実装してみる。 | ||
|
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. 自分が実装した時は、公式ドキュメントとcpythonのソースコードが参考になりました。
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. ありがとうございます。助かります。 |
||
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 を破壊しているのが少し気になりますね。どちらがいいでしょうか。
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.
add をループで回すというのも手です。
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+1番目のスコアや一覧を参照する機能が増えていくことを考えると、nums は破壊しない方が良いと思いました。
__init__でaddを呼び出すということですね。
addで行うのがk個のヒープを作るという初期化のような操作でもあるので、確かにaddでループするのも分かりやすいなと思いました。