703. Kth Largest Element in a Stream.md#11
Conversation
|
|
||
| def add(self, val: int) -> int: | ||
| self.nums.append(val) | ||
| if self.nums == None: |
There was a problem hiding this comment.
self.numsはNoneになり得ないので、この処理はいらないかと思います。
| ``` | ||
| ざっと読んだ。 | ||
| (https://stackoverflow.com/questions/38806202/whats-the-time-complexity-of-functions-in-heapq-library) | ||
| ここまで読んでみたが、なぜstep1よりstep2がこんなに早いのか分からない。。。 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
https://docs.python.org/ja/3.6/library/bisect.html
sortを保って挿入する関数もあります
| timesortは挿入ソートとマージソートのハイブリッド | ||
| (https://en.wikipedia.org/wiki/Timsort) | ||
|
|
||
| O(nlog(n))なら悪くなさそうだけど何でこんなに遅いんだろう。。。 |
There was a problem hiding this comment.
処理全体を通しての時間計算量を求め、そこから処理時間を推定することをお勧めいたします。処理時間の推定方法は、過去に自分がほかの方のソースコードにコメントしたものがありますので、探されることをお勧めいたします。
There was a problem hiding this comment.
There was a problem hiding this comment.
わざわざ探していただきありがとうございます🙇♂️
| step2 | ||
| ```python | ||
| class KthLargest: | ||
| import heapq |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
この代入文だと、処理後入力のリストが書き換えられます。
この関数をブラックボックスのように中身をみずに使うと、利用者にとって予想外の結果になるかもしれません。
リストのコピーを取るなどした方がいいと思いました。
参考コメント
| def __init__(self, k: int, nums: List[int]): | ||
| self.k = k | ||
| if self.k <= 0: | ||
| return print("The value of k is Error!") |
There was a problem hiding this comment.
これは、print した後に、その返り値である None が返ります。
Exception を投げたければ raise です。
|
|
||
| def add(self, val: int) -> int: | ||
| heappush(self.nums,val) | ||
| if self.nums == None: |
There was a problem hiding this comment.
pep8では、== Noneを使うのは推奨されないようです。
https://pep8-ja.readthedocs.io/ja/latest/
None のようなシングルトンと比較をする場合は、常に is か is not を使うべきです。絶対に等値演算子を使わないでください。
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