-
Notifications
You must be signed in to change notification settings - Fork 0
347. Top K Frequent Elements.md #9
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,113 @@ | ||||||||||||
URL: https://leetcode.com/problems/top-k-frequent-elements/description/ | ||||||||||||
|
||||||||||||
# Step 1 | ||||||||||||
|
||||||||||||
- 実装時間: 5分 | ||||||||||||
- 時間計算量: O(n log n) | ||||||||||||
- 空間計算量: O(n) | ||||||||||||
|
||||||||||||
kは条件で負の数は入ってこないので、バリデーションはしない。 | ||||||||||||
|
||||||||||||
```python | ||||||||||||
class Solution: | ||||||||||||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||||||||||||
count_frequency = {} | ||||||||||||
for num in nums: | ||||||||||||
if num in count_frequency: | ||||||||||||
count_frequency[num] += 1 | ||||||||||||
else: | ||||||||||||
count_frequency[num] = 1 | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. step2以降でお気づきですが、default_dictでこのあたりの初期化処理をなくせそうですね |
||||||||||||
|
||||||||||||
top_k_elements = [] | ||||||||||||
for key, value in sorted(count_frequency.items(), key=lambda x:x[1], reverse=True)[:k]: | ||||||||||||
top_k_elements.append(key) | ||||||||||||
Comment on lines
+22
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ループの対象がわかりづらいので、事前に別変数で定義したほうが可読性が上がりそうです。( また
Suggested change
|
||||||||||||
|
||||||||||||
return top_k_elements | ||||||||||||
``` | ||||||||||||
|
||||||||||||
後半は1行でかけると気づいた。 | ||||||||||||
|
||||||||||||
```python | ||||||||||||
class Solution: | ||||||||||||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||||||||||||
count_frequency = {} | ||||||||||||
for num in nums: | ||||||||||||
if num in count_frequency: | ||||||||||||
count_frequency[num] += 1 | ||||||||||||
else: | ||||||||||||
count_frequency[num] = 1 | ||||||||||||
Comment on lines
+35
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これも defaultdict 使わないとしてもいくつかあって、 if num not in count_frequency:
count_frequency[num] = 0
count_frequency[num] += 1 か count_frequency.setdefault(num, 0)
count_frequency[num] += 1 か count_frequency[num] = count_frequency.get(num, 0) + 1 ですかね。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。 以下自分メモ: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あ、そうそう。これを開いて読むの大事です。翻訳でもいいです。 |
||||||||||||
return list(map(lambda x:x[0], sorted(count_frequency.items(), key=lambda x:x[1], reverse=True)[:k])) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一行に詰め込み過ぎており、読むにあたり認知負荷が高く感じられました。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fhiyo/leetcode#17 (comment) それはそうとして、文法への慣れの問題はあると思っています。いくつか案。 list(map(lambda x:x[0], items)) この形は [k for k, _ in items] のほうが見やすいでしょう。 また、sorted を使うならば、 sorted(count_frequency, key=count_frequency.get, reverse=True)[:k] も手です。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。
すごくご指摘通りで、リストの内包表記が慣れないなと思ってるところでした。(書いてあったら読めるんですが、どうも自分で書く際の選択肢に出てこないです。。) |
||||||||||||
``` | ||||||||||||
|
||||||||||||
# Step 2 | ||||||||||||
|
||||||||||||
- 参考にした | ||||||||||||
- https://github.com/tarinaihitori/leetcode/pull/9 | ||||||||||||
- https://github.com/thonda28/leetcode/pull/17 | ||||||||||||
- https://github.com/colorbox/leetcode/pull/24 | ||||||||||||
- https://github.com/rihib/leetcode/pull/20 | ||||||||||||
- https://github.com/hroc135/leetcode/pull/10 | ||||||||||||
- https://github.com/hayashi-ay/leetcode/pull/60/ | ||||||||||||
- https://github.com/kazukiii/leetcode/pull/10 | ||||||||||||
- https://github.com/wf9a5m75/leetcode3/pull/3 | ||||||||||||
- https://github.com/Yoshiki-Iwasa/Arai60/pull/8 | ||||||||||||
- https://github.com/kagetora0924/leetcode-grind/pull/11 | ||||||||||||
- https://github.com/seal-azarashi/leetcode/pull/9 | ||||||||||||
|
||||||||||||
- 変数の名前 | ||||||||||||
- num_to_count のように、変数にどのような値が含まれているかを想像できるような変数名にしたほうが良い | ||||||||||||
- https://github.com/colorbox/leetcode/pull/24/files#r1740739103 | ||||||||||||
- 変数名のnums_frequencyですが、整数に対して頻度なので、num_to_freqencyとかのほうが分かりやすい | ||||||||||||
- https://github.com/Ryotaro25/leetcode_first60/pull/10/files#r1623225196 | ||||||||||||
|
||||||||||||
|
||||||||||||
- 別のパターン | ||||||||||||
- `dict()`のかわりに`defaultdict()`をつかう | ||||||||||||
- https://github.com/thonda28/leetcode/pull/17/files#r1769492893 | ||||||||||||
- top kを選ぶときにheapqを使う。 | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 優先度付きキューとかは割と見るイメージがあるので、(heapq ライブラリではなく)自分で一度実装してみるのもおもしろいと思います。 |
||||||||||||
- バケットソートを使う | ||||||||||||
- https://github.com/kazukiii/leetcode/pull/10#discussion_r1639979474 | ||||||||||||
|
||||||||||||
- lambda関数を使ってる人がいなかった。 | ||||||||||||
- 他の人のコードと見比べると、Step1では`x`が急に出てきているので、わかりにくくてよくない気がした。 | ||||||||||||
- そもそも内包表記でかける | ||||||||||||
- `return [ num for count, num in top_k_frequent]` | ||||||||||||
|
||||||||||||
- 問題設定は必ず k 種類あることになっていますが、なかった場合にどうするかは考えておいてください。 | ||||||||||||
特別な値を返すか、Exception を投げるか、短いものでも返すか、プログラムを止めるか、そのあたりです。 | ||||||||||||
- https://github.com/tarinaihitori/leetcode/pull/9/files#r1816996368 | ||||||||||||
|
||||||||||||
```python | ||||||||||||
class Solution: | ||||||||||||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||||||||||||
num_to_count = defaultdict(int) | ||||||||||||
for num in nums: | ||||||||||||
num_to_count[num] += 1 | ||||||||||||
|
||||||||||||
top_k_frequent = [] | ||||||||||||
for num, count in num_to_count.items(): | ||||||||||||
heappush(top_k_frequent, (count, num)) | ||||||||||||
if len(top_k_frequent) > k: | ||||||||||||
heappop(top_k_frequent) | ||||||||||||
return [num for count, num in top_k_frequent] | ||||||||||||
``` | ||||||||||||
|
||||||||||||
# Step 3 | ||||||||||||
|
||||||||||||
- 時間計算量: O(n log k) | ||||||||||||
- 空間計算量: O(n) | ||||||||||||
|
||||||||||||
```python | ||||||||||||
class Solution: | ||||||||||||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||||||||||||
num_to_count = defaultdict(int) | ||||||||||||
for num in nums: | ||||||||||||
num_to_count[num] += 1 | ||||||||||||
|
||||||||||||
top_k_frequent = [] | ||||||||||||
for num, count in num_to_count.items(): | ||||||||||||
heappush(top_k_frequent, (count, num)) | ||||||||||||
if len(top_k_frequent) > k: | ||||||||||||
heappop(top_k_frequent) | ||||||||||||
return [num for _, num in top_k_frequent] | ||||||||||||
``` |
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.
こっちの書き方のほうが個人的には好きです。(が、趣味の範疇な気がします)