Skip to content

703. Kth Largest Element in a Stream#8

Open
tarinaihitori wants to merge 2 commits into
mainfrom
703-kth-largest-element-in-a-stream
Open

703. Kth Largest Element in a Stream#8
tarinaihitori wants to merge 2 commits into
mainfrom
703-kth-largest-element-in-a-stream

Conversation

@tarinaihitori
Copy link
Copy Markdown
Owner

Comment on lines +50 to +51
else:
return None
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これ、何重にか違和感があります。
対応する if が return しているので else で下げる必要がない。
return None とあるが書かなくても return する。
関数のシグネーチャーは int を返すことになっていて、Optional で None は返らない。
そもそも、k が正ならば nums は空にならないはずだが、k が0以下の場合を考えているのか。しかし、それは init で弾くべきでは。

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.

考慮すべきこととそれへの対処がごっちゃになってしまいました。

return None
```

他の人の解法や[答え](https://leetcode.com/problems/kth-largest-element-in-a-stream/solutions/5624035/min-heap-easy-solution-12ms-beats-98/)を見た。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LeetCode 上の解法を見るのもいいのですが十分でないものも多いです。

他の人の解法を見るところで一番やってほしいことは「自分のコードにどのようなコメントが付くかを予測する」ことです。前に同じ問題を解いた人たちのコードと付いているコメントを見て考えて下さい。

というのも、専門家と同じ物を見たときに、専門家のブレの範囲内で同じことを感じて同じコメントができていたら、専門家と同じ判断ができているということだからです。

読むことは書くことよりもだいぶ大事です。

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.

他の人の解法を見るところで一番やってほしいことは「自分のコードにどのようなコメントが付くかを予測する」ことです。前に同じ問題を解いた人たちのコードと付いているコメントを見て考えて下さい。

他の人の解法を見て満足してしまって、コメントをあまり追えていなかったです。
自分のコードにどのようなコメントが付くかを予測できるように考えたいと思います。

Comment thread 703. Kth Largest Element in a Stream.md Outdated
nums は k 個以上持つ必要がないので、リサイズする処理を入れる。

```python
class BinarySearch:
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.

すみません。途中のコードが入っていましたので、削除しました。


def add(self, val: int) -> int:
self.nums.append(val)
self.nums.sort(reverse=True)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

addする時点ではnumsはソート済みなので再度ソートするのではなく、前からなめていってinsertionポイントを探すほうが良いかもしれないです

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.

そちらのほうが、良さそうです。

https://hayapenguin.com/notes/Posts/2024/04/24/how-to-practice-coding-effectively にもあるようにライブラリを使ったら、それを書けるようにしておく。
4th step で行う予定。
時間計算量:
init: O(nlogn)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

heapifyしてheappopするよりかはheappushで構築しつつ、要素数がkを超えるとheappopする実装に変えると計算量がO(nlogk)になり、kが小さいときにより効率的になると思います。

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.

たしかにheappushで構築したほうが、kが小さいときに特に効率的になりそうですね。
ありがとうございます。

時間計算量:
init: O(nlogn)
add: O(logk)
空間計算量:O(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.

ちょっと自信がないので間違ってたら指摘して欲しいのですが、空間計算量はO(k)にならないのかなと思います。

Total Space Complexity: O(n)
<- 引数のnumsも加味するならO(n)
Auxiliary Space: O(1)
<- 入力を考慮しない空間計算量。self.numsは引数で与えられたnumsの参照なのでこのアルゴリズム自体では追加のメモリとしては定数倍

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.

あー、initでheapのサイズをk以下にするので空間計算量はO(k)だと思ったんですが、
たしかに、引数のnumを考慮するとO(n)になりそうですね。


heapq.heapify(self.nums)

while k < len(self.nums):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

numsの数値を一つづつヒープに取り込むようにすると、その処理内でヒープサイズがkを超えないようにできます。
余分なメモリ消費を抑えれます。

self.k = k
self.nums = sorted(nums, reverse=True)
while len(self.nums) > k:
self.nums.pop()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この部分はself.nums = self.nums[:k+1]でいいのではないでしょうか?

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.

5 participants