Skip to content

Solve 703. Kth Largest Element in a Stream (step 1, 2)#12

Open
cheeseNA wants to merge 2 commits intomainfrom
703
Open

Solve 703. Kth Largest Element in a Stream (step 1, 2)#12
cheeseNA wants to merge 2 commits intomainfrom
703

Conversation

@cheeseNA
Copy link
Copy Markdown
Owner

class KthLargest:
def __init__(self, k: int, nums: List[int]):
sorted_nums = sorted(nums, reverse=True)
self.top_k_min_heap = sorted_nums[:k]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

細かい点となりますが、公開されていないメソッド名やインスタンス変数名には、先頭に _ を付けることをお勧めします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!メソッド名だけ気にしてメンバの変数名については失念していました.

Copy link
Copy Markdown

@nodchip nodchip left a comment

Choose a reason for hiding this comment

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

よいと思います。


- https://discord.com/channels/1084280443945353267/1195700948786491403/1198906989070258277
- "priority queue は一回実装しておいてください。" → 実装します.
- priority queue と heap の違いって何と思って調べたら, priority queue は abstract data type であり, heap はその実装の一つだということだった.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

勉強になります。PythonだとBinary Heapで実装されていますが、Priority Queueの実装としては他にも色々あるんですねー。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

こちらこそいつも勉強になっています!ヒープですとここに様々なものがまとめられてますね!
binary heapよりも計算量が良いものが色々あるみたいです

- priority queue ある.
- [参考 priority queue 実装](https://discord.com/channels/1084280443945353267/1192736784354918470/1194613857046503444)

heap を実装してみるが, エラー処理やどこまでメソッド化するかが難しい.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

CPythonの実装を参考にしてみると良いと思います。
https://github.com/python/cpython/blob/main/Lib/heapq.py

popやpushの実装が大きくなっていますが関数に切り出せそうです。切り出すとheapifyや初期化時に配列を受け取るケースとかを追加で実装しようとする際に比較的楽になると思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!
なるほど, shift up/downとして切り出せるのですね

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

正確にはsift up/downですね。

shift up/downでもそんなに意味が変わらない気がするので紛らわしいですね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

本当ですね, siftという単語を今まで見なかったので気付きませんでした. ありがとうございます

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.

3 participants