-
Notifications
You must be signed in to change notification settings - Fork 0
703. Kth Largest Element in a Stream.md #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?
Conversation
- init: O(nlog n) | ||
- add: O(log n) | ||
- 空間計算量: | ||
- init: O(k) |
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.
sorted(nums, reverse=True)が新しく配列を作るのでStep1はO(n)になるかと思います。
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が負の場合について | ||
- 今回は問題上、出てこない。 | ||
- プラスアルファで検討しても良かった。弾くならinitで弾くべき |
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] のところで、予期しないスライスができるか落ちるかで、中途半端にそれらしい値が出てくることになるでしょう。
あまり意味のないものがそれっぽく動き続けるのは、わりとデバッグのときに困ります。
このあたりは、どういう環境に置くか次第なところはあります。
たとえば、ユーザーフェイシングならば、なんらかの結果が見えたほうがいい場合もあるでしょう。夜中に毎日走るパイプラインなどだったらエラーを出して止まってくれたほうが嬉しいでしょう。
常に使い道からこうしたいまでをつなげてください。
class KthLargest: | ||
def __init__(self, k: int, nums: List[int]): | ||
self.k = k | ||
self.scores_heap = [] |
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.
heapq.heapify() を使うともう少しシンプルになるかと思ったのですが、ならなそうですね…。
def __init__(self, k: int, nums: List[int]): | ||
self.k = k | ||
self.scores_heap = [] | ||
for num in sorted(nums, reverse=True)[:k]: |
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.
好みかもしれませんが、sortedした後にheappushしているのが気になりました。
for文の中でheapq内の要素数がk個を超える時にheappopするとよさそうです。
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.
今回でなくて良いと思いますが、クイックセレクト、ヒープの実装はしておくと良いと思います。
# Step 3 | ||
|
||
- 時間計算量: | ||
- init: O(nlog n) |
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個以上はヒープに入れないようにしているので、O(nlogk)だと思います。
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個以上はヒープに入れないようにしているとはいえ、for num in nums:
でn個みちゃってるので、O(n log n)かなと思ったんですが、どうでしょう。。。
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回あたりの挿入がO(log k)だからってことですよね!なるほどです
|
||
- 時間計算量: | ||
- init: O(nlog n) | ||
- add: O(log n) |
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.
こちらもO(logk)
- init: O(nlog n) | ||
- add: O(log n) | ||
- 空間計算量: | ||
- init: O(k) |
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.
_kth_largest_nums
はインスタンス自体の変数なので、その意味だとO(1)かもしれないです
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.
ごめんなさい・・指摘が理解できてないです 🙇
インスタンス自体の変数だと追加で使用する容量は固定ということ・・?
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.
_kth_largest_nums
はインスタンスの変数として定義されているので、init
関数としては追加のメモリは必要としていないのではないでしょうかということでした。
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.
あぁ!ありがとうございます、おっしゃりたいことわかりました。
ちょっと悩ましいのが、僕のこの実装だと、__init__
の中で、add
関数をn回呼ぶので、
「init
関数単体ではO(1)とも言える。けど、呼び先のadd
も入れるとinit
の処理終了までに、結局(最大で)要素k個分の領域を使用する」
みたいな感じです。
なので、処理終了までの話を見たときには、
- init: O(k)
- add: O(1)
だし、純粋にその関数固有の処理を見たときには
- init: O(1)
- add: O(k)
と書いた方がいいのか。と気付きました。
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.
まだ自分の認識とあっていないです。
_kth_largest_nums
はinitともaddとも違う場所に保存されているので、init,addからheappushなどを読んだとしてもinit, add関数としては追加のメモリを使用しないというつもりでした。
URL: https://leetcode.com/problems/kth-largest-element-in-a-stream/description/