Skip to content

Solved: 703. Kth Largest Element in a Stream#8

Open
Fuminiton wants to merge 1 commit intomainfrom
solve-problem8
Open

Solved: 703. Kth Largest Element in a Stream#8
Fuminiton wants to merge 1 commit intomainfrom
solve-problem8

Conversation

@Fuminiton
Copy link
Copy Markdown
Owner

return self.top_k[0]
```
### 感想
- たまたまnumsが空で来た時に対処できるコードになっていただけで、step1で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.

最近はじまったオープンソースクイズですが、こういったところのイシューをみてもらうと、最終的に、これくらいの精度でコードが読解されるのだということが分かるかと思います。
https://discord.com/channels/1084280443945353267/1340007089291919393/1340012211430625324

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 __init__(self, k: int, nums: List[int]):
self.k = k
self.top_k = nums
heapq.heapify(self.top_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.

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.

add をループで回すというのも手です。

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.

nums を破壊しているのが少し気になりますね。どちらがいいでしょうか。

今後、k+1番目のスコアや一覧を参照する機能が増えていくことを考えると、nums は破壊しない方が良いと思いました。

add をループで回すというのも手です。

__init__でaddを呼び出すということですね。
addで行うのがk個のヒープを作るという初期化のような操作でもあるので、確かにaddでループするのも分かりやすいなと思いました。

return self.top_k[0]
```
### 感想
- すんなり解けたので、次は binary heap を実装してみる。
Copy link
Copy Markdown

@quinn-sasha quinn-sasha Feb 18, 2025

Choose a reason for hiding this comment

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

自分が実装した時は、公式ドキュメントとcpythonのソースコードが参考になりました。

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 on lines +65 to +67
heapq.heappush(self.top_k, val)
while len(self.top_k) > self.k:
heapq.heappop(self.top_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.

heappushpopを使うと、ここの処理がより効率的になります
https://docs.python.org/ja/3.13/library/heapq.html#heapq.heappushpop

def __init__(self, k: int, nums: List[int]):
self.k = k
self.top_k = nums
heapq.heapify(self.top_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.

heapqは省略したほうが見やすいかもしれません。

Copy link
Copy Markdown

@olsen-blue olsen-blue Feb 23, 2025

Choose a reason for hiding this comment

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

普通の環境であれば下記のように個別の関数をインポートしておくと、

from heapq import heapify
nums = [4, 5, 8, 2]
heapify(nums)  # heapq. を省略できる

みたいに先頭の heapq. を省略できるということですね。(他の関数も同様)
heapって何回も書くの大変で避けたかったので助かります。シンプルになって良いですね。

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