Skip to content
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

703. Kth Largest Element in a Stream.md #11

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

lilnoahhh
Copy link
Owner

@lilnoahhh lilnoahhh commented Mar 4, 2025

703. Kth Largest Element in a Stream
You are part of a university admissions office and need to keep track of the kth highest test score from applicants in real-time. This helps to determine cut-off marks for interviews and admissions dynamically as new applicants submit their scores.

You are tasked to implement a class which, for a given integer k, maintains a stream of test scores and continuously returns the kth highest test score after a new score has been submitted. More specifically, we are looking for the kth highest score in the sorted list of all scores.

Implement the KthLargest class:

KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of test scores nums.
int add(int val) Adds a new test score val to the stream and returns the element representing the kth largest element in the pool of test scores so far.
次はTop K Frequent Elements


def add(self, val: int) -> int:
self.nums.append(val)
if self.nums == None:

Choose a reason for hiding this comment

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

self.numsはNoneになり得ないので、この処理はいらないかと思います。

```
ざっと読んだ。
(https://stackoverflow.com/questions/38806202/whats-the-time-complexity-of-functions-in-heapq-library)
ここまで読んでみたが、なぜstep1よりstep2がこんなに早いのか分からない。。。
Copy link

Choose a reason for hiding this comment

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

https://note.com/map1000da/n/n02d2fefa4343
公式ドキュメントが見つからなかったのですが、
heappush,heappopはO(logN)でできるみたいです。
sortはO(NlogN)なのでそれで差が出ているのかもしれません。

self.nums.append(val)
if self.nums == None:
return None
self.nums.sort(reverse=True)
Copy link

Choose a reason for hiding this comment

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

https://docs.python.org/ja/3.6/library/bisect.html
sortを保って挿入する関数もあります

timesortは挿入ソートとマージソートのハイブリッド
(https://en.wikipedia.org/wiki/Timsort)

O(nlog(n))なら悪くなさそうだけど何でこんなに遅いんだろう。。。
Copy link

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.

Copy link
Owner Author

Choose a reason for hiding this comment

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

わざわざ探していただきありがとうございます🙇‍♂️

step2
```python
class KthLargest:
import heapq
Copy link

Choose a reason for hiding this comment

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

import は class 内では行わないことが多いと思います。

https://google.github.io/styleguide/pyguide.html#22-imports

Use import statements for packages and modules only, not for individual types, classes, or functions.

self.k = k
if k <= 0:
return print("The value of k is Error")
self.nums = nums

Choose a reason for hiding this comment

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

この代入文だと、処理後入力のリストが書き換えられます。
この関数をブラックボックスのように中身をみずに使うと、利用者にとって予想外の結果になるかもしれません。
リストのコピーを取るなどした方がいいと思いました。
参考コメント

def __init__(self, k: int, nums: List[int]):
self.k = k
if self.k <= 0:
return print("The value of k is Error!")
Copy link

Choose a reason for hiding this comment

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

これは、print した後に、その返り値である None が返ります。
Exception を投げたければ raise です。


def add(self, val: int) -> int:
heappush(self.nums,val)
if self.nums == None:

Choose a reason for hiding this comment

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

pep8では、== Noneを使うのは推奨されないようです。
https://pep8-ja.readthedocs.io/ja/latest/

None のようなシングルトンと比較をする場合は、常に is か is not を使うべきです。絶対に等値演算子を使わないでください。

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.

7 participants