Skip to content

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

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

Kth Largest Element in a Stream #9

wants to merge 1 commit into from

Conversation

bumbuboon
Copy link
Owner


```

タイピングのミスがめっちゃ多い。タイピングを鍛えるにはたくさん書くしかないのだろうか。

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

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よりも長くならないことはクラス全体を見ないと分からないからです。

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個になるまで削除するということなので確かにwhileの方が適切であるように思いました。

class KthLagest:

def __init__(self, k: int, nums: List[int]):
self.k = k
Copy link

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とは
Copy link

Choose a reason for hiding this comment

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

nits:些細ですが。

Suggested change
- heap queとは
- heap queueとは

https://docs.python.org/3/library/heapq.html

@katataku
Copy link

katataku commented Dec 4, 2024

必須じゃないとは思うんですが、
他の方は計算量を考えて併記されていることが多いと思うので、考えてみると学びがあるかもです。
(次からでいいと思います。)

```python
class KthLargest:

def __init_(self, k: int, nums: List[int])
Copy link

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)
Copy link

Choose a reason for hiding this comment

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

入力として与えられている nums の中身を変更している点が気になりました。このクラスを呼び出す側の立場に立って考えてみると、与えた nums の中身が変更されるのは、意図しない動作に感じられるのではないかと思います。そのような場合は、 nums のコピーを保持し、そちらを変更するようにするか、コメントで nums の中身を変更することを明示したほうが良いと思います。

Copy link
Owner Author

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

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.

Comment on lines +83 to +85
[C++であればmultisetが使える](https://github.com/haniwachann/leetcode/pull/1/files/991655bbb5a07f7c508494233b6b22494d5517df#diff-6f3d970863dc500b1778101efd01e2a76334accabb99be3850492a4e4c30240d)
- pythonに同じようなライブラリーはないのだろうか
- 外部ライブラリーに[SortedList](https://grantjenks.com/docs/sortedcontainers/sortedlist.html)がある
Copy link

Choose a reason for hiding this comment

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

見た感じ内部実装はちょっと違いそうですね
C++のmultisetは一般的には赤黒木という平衡二分探索木のデータ構造で実装されます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

自動でソートされるという機能のみに注目しており、内部実装まではきちんと比較しておりませんでした。コメントありがとうございます。

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