Skip to content

347. Top K Frequent Elements#10

Open
MA-yo-TA wants to merge 1 commit into
mainfrom
347-Top-K-Frequent-Elements
Open

347. Top K Frequent Elements#10
MA-yo-TA wants to merge 1 commit into
mainfrom
347-Top-K-Frequent-Elements

Conversation

@MA-yo-TA

Copy link
Copy Markdown
Owner

Comment on lines +11 to +15
- カウント部分が O(n)
- ヒープ化が O(n)
- 取り出すところが pop O(log n) をk 回で O(k log n)

なので O(k * log n)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

時間計算量は O(n + k log n) のほうがより丁寧だと思いました.

n = k, n <= k などの場合でそれぞれ異なるように思います.

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.

ありがとうございます。特にkが小さい場合などはnの方が大きく効いてくるので、そちらが正しそうですね。

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
# counter の value は [回数, 値]のリスト
counter: Dict[int, List[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.

妙案が思い浮かばないんですが, counter という名前を少し変更したい気持ちになっています.
counterという名称から推測するよりも構造が複雑だったので.

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.

確かにそうですね
他の方にいただいたコメントのように、構造の方を単純にしてしまうというのが今回はいいのかもしれません:
#10 (comment)

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_count_and_num という名前は思いついたのですが、野暮ったく感じました。構造のほうを単純にしてしまったほうが良いと思います。

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.

ありがとうございます。構造が複雑だとどうしても名前も複雑になりそうで、名前を工夫するより大元を解決するようが良さそうですね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この方のPRも,いろんな解法のパターンが記載されていて,個人的には勉強になりましたので貼っておきます
https://github.com/dorxyxki/arai60/pull/9/changes

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.

ありがとうございます。参考にします。

else:
counter[num][0] += 1

count_and_num = list(counter.values())

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値: 出現回数で持っておいて、count_and_numを作るときに

count_and_num = [(count, num) for num, count in counter.items()]

と組にするのはどうでしょうか。
keyとvalueでnumが重複している冗長さが消えるので、個人的にはこちらが良いと感じます。

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.

ありがとうございます。私も必要以上に複雑な構造になっていそうだったのが気になっていましたがそのように書くのがシンプルで良さそうです。

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