-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
|
||
res = [] | ||
while len(res) < k: | ||
res.append(heapq.heappop(heap)[1]) |
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.
私はこれ2行にしますね。
[1]が何なのか思い出す必要があるのと、目が左右に振られるので。
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.
確かに解答を読んだ時に「これなんだっけな...」となりました。同様の書き方は避けようと思います。
|
||
sorted_numbers_of_appearance = sorted(numbers_of_appearance, key=numbers_of_appearance.get, reverse=True) | ||
|
||
top_k_elements = sorted_numbers_of_appearance[: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.
こっちは return とくっつけてもいいかもしれません。
```Python | ||
class Solution: | ||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
numbers_of_appearance = defaultdict(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.
変数名を短くするなら num_to_counts とかでもいいかなと思いました。
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.
同じ感覚です。辞書の変数名は「(キー)to(値)」というのが今の自分的にはしっくりきます。
(なお、私のPRはそうなっていませんでした。)
- 全く知らなかった操作もあったとはいえ、過去の問題に連想が行ったらrinostさんのstep1みたいなアプローチが出来たのか…。あの時辞書の操作がしっくりきておらず、結果として記憶に残らなかった感じがある。 | ||
- 全部理解できなくてもいいや精神で一度公式ドキュメント¶に目を通しとこう。 | ||
- Step1の回答にあった-1をかけてheappopした値をappendする方法より、heappopで頻度の小さい値を除外する方が手間が少なそう | ||
- Defaultdictを使うとキーの初期化なしで頻度を数えれるのか¶ |
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.
collection.Counterや setdefault()などいろいろな書き方があると思うのでそれらを考えても面白いかもしれません。
sorted_frequencies = sorted( | ||
frequencies_of_appearance, key=frequencies_of_appearance.get, reverse=True | ||
) |
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.
横に長くなっているので、縦に並べるのも手かなと思います。
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 | |
) |
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.
- Defaultdictを使うとキーの初期化なしで頻度を数えれるのか¶ | ||
- 文字の種類がk以下な入力は確かにありそう。みんなValueErrorをいれてる。 | ||
- FYさんのステップ5が自然に頭に入ってきたのでこれをお手本にしたい | ||
- Step1で参照した回答ではheapという名前が使われてたけど、実際にはheapにはtop_k_hogeみたいな名前が似合うことが多い印象(これまで見てきたものがpopすることを見越してたからな気がする) |
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.
自分が変数名に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) |
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.
出現回数を直訳したのだと思いますが、長いわりに何の出現回数か何のための変数かが伝わらないかと思います。
num_to_times とかどうでしょうか。
numbers_of_appearance[num] += 1 | ||
|
||
if numbers_of_appearance < k: | ||
raise ValueError('Fewer types of letters appear in input 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.
例外処理を想定することは実務上は有益だと感じました。
ただ、今回の関数は頻度の多い数字を上位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 未満かを判定することもできますしね。
問題文:https://leetcode.com/problems/top-k-frequent-elements/description/