Conversation
| ## step2 | ||
| 自分としては気になるところはないので、他の人のコードを見る | ||
| - https://github.com/Hiroto-Iizuka/coding_practice/pull/9/files | ||
| - Counterだと使うだけなので出題意図と沿っていないかも |
There was a problem hiding this comment.
出題意図と沿っていない
というより、じゃあ Counter 実装してみてください、という話になるのだと思います。
(でstep2ではそうできています)
|
|
||
|
|
||
| ## TODO | ||
| [] クイックソートについて |
There was a problem hiding this comment.
コーディング練習会の本筋ではないですが、Markdownのチェックボックスは箇条書きでないとレンダリングされないようです。
| [] クイックソートについて | |
| - [] クイックソートについて |
| for num in nums: | ||
| num_to_frequency[num] += 1 | ||
|
|
||
| num_frequency_order = sorted( |
There was a problem hiding this comment.
細かくて恐縮ですが、英語的に _ordered がより適切に感じました。_order という名前だけ見ると、ソート後のインデックスが入っているようにも読めるので
There was a problem hiding this comment.
たしかに、nums_ordered_by_frequencyとかだとかなり丁寧な印象ですね。
| # [347] Top K Frequent Elements | ||
| # | ||
|
|
||
| from collections import defaultdict |
There was a problem hiding this comment.
[fyi]
(まだでしたら)インポートの流儀も気にすると良いかもしれません。
例えばGoogleのスタイルガイドでは、ここでは import collections が推奨されているように読めます。
https://google.github.io/styleguide/pyguide.html#22-imports
Use import statements for packages and modules only, not for individual types, classes, or functions.
There was a problem hiding this comment.
もちろんGoogleのスタイルガイドが絶対ではありません。メリット・デメリットを把握しつつ、自分が取るスタイルを決めると良いと思います。
(私も個人的には、コード本体が長くなりがちなのでこのような短いimportの方が好きです)
| - 末尾再帰最適化 | ||
| - ピボット選択 | ||
| - マージソートとのプロコン | ||
| - このかたのmemoはすごいが、自分の力量だとここまですると1問につき1年かかるので、1周したあとまた見にくる |
There was a problem hiding this comment.
私、「マージソートとのプロコン」という言葉に、ソートの安定性やメモリーアクセスの局所性などを詰め込んでいます。本当は、中身を詳しく書いたほうがいいですね。
| - 内部で_heapq.nlargestを使っており、計算量はO(nlogk) | ||
|
|
||
| ```py | ||
| for num, _count in counter.most_common(): |
There was a problem hiding this comment.
確かに、たとえばPEP8ではcountが予約語ならcount_のようによける書き方がありますが、countは予約語では無いのでそのまま書いて大丈夫かなと思いました。
If a function argument’s name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus class_ is better than clss. (Perhaps better is to avoid such clashes by using a synonym.)
https://peps.python.org/pep-0008/#function-and-method-arguments
あるいは使わないループ変数という意図であれば_で潰すと良いと思います。
| if len(frequent_nums) == k: | ||
| break | ||
| ``` | ||
| - 上記のように書いたが、forのindexをとるときは普段enumerateを使うようにしているが、今回の場合は下記のようになり少しわかりにくいかなと思って上記にした |
There was a problem hiding this comment.
自分も上の方がわかりやすいと感じます。
簡単なものさしとして、変数は減らせるだけ減らすのはわりと有効な気がします。(もちろん例外もありますが。)
| for num in nums: | ||
| num_to_frequency[num] += 1 | ||
|
|
||
| num_frequency_order = sorted( |
There was a problem hiding this comment.
たしかに、nums_ordered_by_frequencyとかだとかなり丁寧な印象ですね。
| for num, _count in counter.most_common(): | ||
| frequent_nums.append(num) | ||
| if len(frequent_nums) == k: | ||
| break |
There was a problem hiding this comment.
Counter.most_common()メソッドのオプショナルな引数nで上位n個だけにできるので、使うとif-breakのところが無くせてシンプルです。
| for num, _count in counter.most_common(): | |
| frequent_nums.append(num) | |
| if len(frequent_nums) == k: | |
| break | |
| for num, _count in counter.most_common(k): | |
| frequent_nums.append(num) |
https://docs.python.org/3/library/collections.html#collections.Counter.most_common
こう書くなら、内包表記にしてもいいですね。趣味の範囲です。
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
counter = Counter(nums)
return [num for num, _ in counter.most_common(k)]
解く問題
Top K Frequent Elements
次に解く問題
Find K Pairs With Smallest Sums