Skip to content

Create Top_K_Frequent_Elements.md #11

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 3 commits into
base: main
Choose a base branch
from

Conversation

SanakoMeine
Copy link
Owner

@SanakoMeine SanakoMeine commented Jan 22, 2025


res = []
while len(res) < k:
res.append(heapq.heappop(heap)[1])
Copy link

Choose a reason for hiding this comment

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

私はこれ2行にしますね。
[1]が何なのか思い出す必要があるのと、目が左右に振られるので。

https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.kqo7dp4vlnzh

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かに解答を読んだ時に「これなんだっけな...」となりました。同様の書き方は避けようと思います。


sorted_numbers_of_appearance = sorted(numbers_of_appearance, key=numbers_of_appearance.get, reverse=True)

top_k_elements = sorted_numbers_of_appearance[:k]
Copy link

Choose a reason for hiding this comment

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

こっちは return とくっつけてもいいかもしれません。

```Python
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
numbers_of_appearance = defaultdict(int)

Choose a reason for hiding this comment

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

変数名を短くするなら num_to_counts とかでもいいかなと思いました。

Copy link

@olsen-blue olsen-blue Jan 26, 2025

Choose a reason for hiding this comment

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

同じ感覚です。辞書の変数名は「(キー)to(値)」というのが今の自分的にはしっくりきます。
(なお、私のPRはそうなっていませんでした。)

- 全く知らなかった操作もあったとはいえ、過去の問題に連想が行ったらrinostさんのstep1みたいなアプローチが出来たのか…。あの時辞書の操作がしっくりきておらず、結果として記憶に残らなかった感じがある。
- 全部理解できなくてもいいや精神で一度公式ドキュメント¶に目を通しとこう。
- Step1の回答にあった-1をかけてheappopした値をappendする方法より、heappopで頻度の小さい値を除外する方が手間が少なそう
- Defaultdictを使うとキーの初期化なしで頻度を数えれるのか¶

Choose a reason for hiding this comment

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

collection.Counterや setdefault()などいろいろな書き方があると思うのでそれらを考えても面白いかもしれません。

Comment on lines +58 to +60
sorted_frequencies = sorted(
frequencies_of_appearance, key=frequencies_of_appearance.get, reverse=True
)
Copy link

Choose a reason for hiding this comment

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

横に長くなっているので、縦に並べるのも手かなと思います。

Suggested change
sorted_frequencies = sorted(
frequencies_of_appearance, key=frequencies_of_appearance.get, reverse=True
)
sorted_frequencies = sorted(
frequencies_of_appearance,
key=frequencies_of_appearance.get,
reverse=True
)

Copy link

Choose a reason for hiding this comment

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

- Defaultdictを使うとキーの初期化なしで頻度を数えれるのか¶
- 文字の種類がk以下な入力は確かにありそう。みんなValueErrorをいれてる。
- FYさんのステップ5が自然に頭に入ってきたのでこれをお手本にしたい
- Step1で参照した回答ではheapという名前が使われてたけど、実際にはheapにはtop_k_hogeみたいな名前が似合うことが多い印象(これまで見てきたものがpopすることを見越してたからな気がする)
Copy link

Choose a reason for hiding this comment

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

自分が変数名にheapと入れていないのは、わざわざheapq.heappushとか明らかにデータ構造がわかる書き方をするのに、名前にheapと入れる必要はないなくらいの気持ちで入れてないです。

numsをわざわざnums_listとしないくらいの気持ちです。

```Python
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
frequencies_of_appearance = defaultdict(int)
Copy link

Choose a reason for hiding this comment

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

出現回数を直訳したのだと思いますが、長いわりに何の出現回数か何のための変数かが伝わらないかと思います。
num_to_times とかどうでしょうか。

numbers_of_appearance[num] += 1

if numbers_of_appearance < k:
raise ValueError('Fewer types of letters appear in input nums')
Copy link

Choose a reason for hiding this comment

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

例外処理を想定することは実務上は有益だと感じました。
ただ、今回の関数は頻度の多い数字を上位k個まで出すというものなので、個人的には例外を発生させなくてもよいかな、という認識です。
前もって出現する数字の数を把握しないといけなくなるので、使いづらいと思います

Copy link

Choose a reason for hiding this comment

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

戻ってきてから k 未満かを判定することもできますしね。

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