Skip to content

347. Top K Frequent Elements#60

Open
hayashi-ay wants to merge 9 commits intomainfrom
hayashi-ay-patch-49
Open

347. Top K Frequent Elements#60
hayashi-ay wants to merge 9 commits intomainfrom
hayashi-ay-patch-49

Conversation

@hayashi-ay
Copy link
Copy Markdown
Owner

@hayashi-ay hayashi-ay commented Mar 24, 2024

```python
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
freq = defaultdict(int)
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_to_freqencies といった名前にするとよいと思います。

freq[sorted_index], freq[right] = freq[right], freq[sorted_index]
return sorted_index

def quick_select(freq, left, right, 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.

個人的には開閉区間で扱うほうが一般的だと思います。実装上閉区間で扱ったほうが扱いやすいのであれば、その旨をソースコードで書くとよいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。rangeとかも右側が開区間ですしね。書いてみましたが、そこまで実装の手間は変わらなかったです。

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.

2 participants