Skip to content

347. Top K Frequent Elements#10

Open
tNita wants to merge 1 commit into
mainfrom
add/346_top_k_frequent_elements
Open

347. Top K Frequent Elements#10
tNita wants to merge 1 commit into
mainfrom
add/346_top_k_frequent_elements

Conversation

@tNita
Copy link
Copy Markdown
Owner

@tNita tNita commented Feb 8, 2026

- クイックソートはin-placeのソートなので空間計算量O(n)のマージソートよりメモリ使用量は小さい
- 実用面ではクイックソートの方が早いことが多いらしい
- https://stackoverflow.com/questions/70402/why-is-quicksort-better-than-mergesort
- クイックソートはin-placeのソートなのでアクセスするデータが局所的→PUのキャッシュに乗りやすくて早い?(理解怪しい)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ハードディスク上だとクイックソートはとても遅いという話もありますね。
局所化されているが飛び飛びの書き込みが必要になるので。

num_count = defaultdict(int)
for num in nums:
num_count[num] += 1
return sorted(num_count, key=num_count.get, reverse=True)[: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.

(レビューではないのですがmm)
golang でも同じように書けないかなと思って調べたら最近できるようになっていたことを知りました。
勉強になりました。ありがとうございました。
slices package - slices - Go Packages

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.

3 participants