Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

katataku
Copy link
Owner

- init: O(nlog n)
- add: O(log n)
- 空間計算量:
- init: O(k)

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)になるかと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

空間計算量の方ですね、たしかにです!!ありがとうございます!!


- kが負の場合について
- 今回は問題上、出てこない。
- プラスアルファで検討しても良かった。弾くならinitで弾くべき
Copy link

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 = []
Copy link

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]:
Copy link

@colorbox colorbox Nov 27, 2024

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するとよさそうです。

Copy link

@hayashi-ay hayashi-ay left a 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)

Choose a reason for hiding this comment

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

k個以上はヒープに入れないようにしているので、O(nlogk)だと思います。

Copy link
Owner Author

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)かなと思ったんですが、どうでしょう。。。

Copy link
Owner Author

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)

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)

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)かもしれないです

Copy link
Owner Author

Choose a reason for hiding this comment

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

ごめんなさい・・指摘が理解できてないです 🙇

インスタンス自体の変数だと追加で使用する容量は固定ということ・・?

Choose a reason for hiding this comment

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

_kth_largest_numsはインスタンスの変数として定義されているので、init関数としては追加のメモリは必要としていないのではないでしょうかということでした。

Copy link
Owner Author

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)
    と書いた方がいいのか。と気付きました。

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関数としては追加のメモリを使用しないというつもりでした。

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.

6 participants