-
Notifications
You must be signed in to change notification settings - Fork 0
Kth Largest Element in a Stream #9
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
|
||
``` | ||
|
||
タイピングのミスがめっちゃ多い。タイピングを鍛えるにはたくさん書くしかないのだろうか。 |
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.
まあ実際にはエディタ側の補完などもあるので、そこまで気にしなくても良いかもしれないです。
どの程度かにもよりますが、Typingの練習をしても良いかもですね。このサイトだと記号の練習もできて良いと思います。
https://www.typingclub.com/
|
||
def add(self, val: int) -> int: | ||
heapq.heappush(self.kth_largest_nums, val) | ||
if len(self._kth_largest_nums) > self.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
ですかね。
個人的には while の方が意図がはっきりする気はします。if だと、これがself.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.
結果的に一つ削除すれば十分なだけで、やりたいこととしては要素がk個になるまで削除するということなので確かにwhileの方が適切であるように思いました。
class KthLagest: | ||
|
||
def __init__(self, k: int, nums: List[int]): | ||
self.k = 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.
k が 0 だと add で困りそうです。
- 放置
- Exception を投げる
- プログラムを止める
- 特殊な値を返す
などいくつか対応があり状況次第です。何が好ましいか自問自答する癖をつけて想像しておきましょう。
|
||
|
||
- [heapというものがあるらしい](https://github.com/tarinaihitori/leetcode/pull/8/files/b93a4ee64137e37666b03b99b6c0602ce8cf2a10#diff-6e7e84a8c0c13aabe8bad58b32c7215762116a974e2143b20ee931bf24b51a2f) | ||
- heap queとは |
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.
nits:些細ですが。
- heap queとは | |
- heap queueとは |
必須じゃないとは思うんですが、 |
```python | ||
class KthLargest: | ||
|
||
def __init_(self, k: int, nums: List[int]) |
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.
nit: __init__
self.k = k | ||
self.nums = nums | ||
|
||
heapq.heapify(self.nums) |
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.
入力として与えられている nums の中身を変更している点が気になりました。このクラスを呼び出す側の立場に立って考えてみると、与えた nums の中身が変更されるのは、意図しない動作に感じられるのではないかと思います。そのような場合は、 nums のコピーを保持し、そちらを変更するようにするか、コメントで nums の中身を変更することを明示したほうが良いと思います。
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.
その視点はなかったです。ありがとうございます。
```python | ||
class KthLagest: | ||
|
||
def __init__(self, k: int, nums: List[int]): |
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.
タブでインデントをするのは、最近ではあまり見かけないように思います。スペースでインデントすることをお勧めいたします。
https://peps.python.org/pep-0008/#tabs-or-spaces
Spaces are the preferred indentation method.
Tabs should be used solely to remain consistent with code that is already indented with tabs.
https://google.github.io/styleguide/pyguide.html#s3.4-indentation
Indent your code blocks with 4 spaces.
Never use tabs.
[C++であればmultisetが使える](https://github.com/haniwachann/leetcode/pull/1/files/991655bbb5a07f7c508494233b6b22494d5517df#diff-6f3d970863dc500b1778101efd01e2a76334accabb99be3850492a4e4c30240d) | ||
- pythonに同じようなライブラリーはないのだろうか | ||
- 外部ライブラリーに[SortedList](https://grantjenks.com/docs/sortedcontainers/sortedlist.html)がある |
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.
見た感じ内部実装はちょっと違いそうですね
C++のmultisetは一般的には赤黒木という平衡二分探索木のデータ構造で実装されます。
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.
自動でソートされるという機能のみに注目しており、内部実装まではきちんと比較しておりませんでした。コメントありがとうございます。
問題
https://leetcode.com/problems/kth-largest-element-in-a-stream/description/