Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions problme8/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
## 取り組み方
- step1: 5分以内に空で書いてAcceptedされるまで解く
- step2: discordとwebで検索した結果を踏まえ、コードの洗練を洗練させる + 何を悪いと思ったか、何を避けたいと思ったか等感じたことを記載する
- step3: 10分以内に1回もエラーを出さずに3回連続で解く
- step4: binary heapを実装して解く

## step1
```python
class KthLargest:
def __init__(self, k: int, nums: List[int]):
self.k = k
self.from_top_to_k = nums
heapq.heapify(self.from_top_to_k)

def add(self, val: int) -> int:
heapq.heappush(self.from_top_to_k, val)
while len(self.from_top_to_k) > self.k:
heapq.heappop(self.from_top_to_k)
return self.from_top_to_k[0]
```
### 思考過程
step1: heapqを用いてスコアのをヒープを管理する
step2: 要素の数をk個まで減らせば最小ヒープ = k番目に大きい数になる

- 値を負にして格納する方針もあるが、ミスりそう

N を nums.length として、
時間計算量は
- valの挿入: O(logN)
- nums.length - k 回の削除: O(NlogN)
- 最小ヒープの取得: O(1)
となるので、O(NlogN)。
空間計算量はヒープの使用でO(N)。

### 感想
- numsのデータを削除しないで取っておいて、別のヒープを作った方がスコアの一覧をみたいとなった場合も考慮できて丁寧なのかもしれない

## step2
```python
class KthLargest:
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でループするのも分かりやすいなと思いました。


def add(self, val: int) -> int:
heapq.heappush(self.top_k, val)
while len(self.top_k) > self.k:
heapq.heappop(self.top_k)
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.

レビューありがとうございます。

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

簡単なクイズに参加して、感覚掴もうと思います。ありがとうございます。

- heappushをheappushpopに使用としたことで気づいた

## step3
```python
class KthLargest:
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って何回も書くの大変で避けたかったので助かります。シンプルになって良いですね。


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

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.

ありがとうございます。助かります。