Skip to content

347: Top K Frequent Elements#11

Open
xbam326 wants to merge 3 commits into
mainfrom
347
Open

347: Top K Frequent Elements#11
xbam326 wants to merge 3 commits into
mainfrom
347

Conversation

@xbam326
Copy link
Copy Markdown
Owner

@xbam326 xbam326 commented Jan 7, 2026

## step2
自分としては気になるところはないので、他の人のコードを見る
- https://github.com/Hiroto-Iizuka/coding_practice/pull/9/files
- Counterだと使うだけなので出題意図と沿っていないかも
Copy link
Copy Markdown

@mamo3gr mamo3gr Jan 7, 2026

Choose a reason for hiding this comment

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

出題意図と沿っていない

というより、じゃあ Counter 実装してみてください、という話になるのだと思います。
(でstep2ではそうできています)



## TODO
[] クイックソートについて
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

コーディング練習会の本筋ではないですが、Markdownのチェックボックスは箇条書きでないとレンダリングされないようです。

Suggested change
[] クイックソートについて
- [] クイックソートについて

for num in nums:
num_to_frequency[num] += 1

num_frequency_order = sorted(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

細かくて恐縮ですが、英語的に _ordered がより適切に感じました。_order という名前だけ見ると、ソート後のインデックスが入っているようにも読めるので

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_ordered_by_frequencyとかだとかなり丁寧な印象ですね。

# [347] Top K Frequent Elements
#

from collections import defaultdict
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

もちろんGoogleのスタイルガイドが絶対ではありません。メリット・デメリットを把握しつつ、自分が取るスタイルを決めると良いと思います。

(私も個人的には、コード本体が長くなりがちなのでこのような短いimportの方が好きです)

- 末尾再帰最適化
- ピボット選択
- マージソートとのプロコン
- このかたのmemoはすごいが、自分の力量だとここまですると1問につき1年かかるので、1周したあとまた見にくる
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.nlargestを使っており、計算量はO(nlogk)

```py
for num, _count in counter.most_common():
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

確かに、たとえば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を使うようにしているが、今回の場合は下記のようになり少しわかりにくいかなと思って上記にした
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分も上の方がわかりやすいと感じます。

簡単なものさしとして、変数は減らせるだけ減らすのはわりと有効な気がします。(もちろん例外もありますが。)

for num in nums:
num_to_frequency[num] += 1

num_frequency_order = sorted(
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_ordered_by_frequencyとかだとかなり丁寧な印象ですね。

Comment on lines +8 to +11
for num, _count in counter.most_common():
frequent_nums.append(num)
if len(frequent_nums) == k:
break
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Counter.most_common()メソッドのオプショナルな引数nで上位n個だけにできるので、使うとif-breakのところが無くせてシンプルです。

Suggested change
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)]

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.

4 participants