-
Notifications
You must be signed in to change notification settings - Fork 0
347. Top K Frequent Elements #24
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?
Conversation
int count = numToCount.getOrDefault(num, 0) + 1; | ||
numToCount.put(num, count); |
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.
私はこれ2行に分ければそれほど違和感がないですが、単に「なかったら0で初期化して」「1を足す」という分け方でもいいかと思います。ここらへんにいくつかオプションを書いておきました。
https://discord.com/channels/1084280443945353267/1300342682769686600/1357378682163036160
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.
やりたいことを日本語で表現してみるのいいですね。 今後一度立ち止まって考えてみようと思います。
また、オプションも大変参考になります。ありがとうございます。
- まず各数字の出現回数を HashMap に記録する。その後、Map の key(= ユニークな数字の set)を配列に格納する | ||
- Quick Select アルゴリズムを用いて、出現回数に基づいて配列をパーティショニングする | ||
- 任意の pivot 要素 を用意し pivot より大きいグループと小さいグループに配列を分割する | ||
- 上位 k 個の要素がパーティションより右側に来るように繰り返す |
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.
そうですね。left, right が何を意味しているとしてループを回るたびに引き継いでいるかは考えてみましょう。
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.
leftとrightは探索対象となるサブ配列の開始/終了インデックスですね。ループのたびにサブ配列の左右端を更新し、上位k要素が右半分に集まるまで範囲を狭めています。
|
||
- Step1 の改良版。解法は同じ。 | ||
- Step1 では heap に[数値, 回数]ペアを表す int[]を丸ごと入れる、一方で改良版は key(Integer)だけを入れ、heap 作成時の設定で Map を参照する事で頻度を取得している | ||
- 配列でペアを作る方法は暗黙的な使い道の解釈を読み手に強制する点で読み手側の負担が大きいと感じる |
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.
そうですね、こっちの方が読みやすかったです
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.
ありがとうございます。
} | ||
|
||
private int partition(List<Integer> nums, int left, int right, Map<Integer, Integer> numToCount) { | ||
// Using the rightmost element as pivot |
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.
ソート済み、かつkが大きい時に時間かかるという欠点はありそうですね。
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.
おっしゃるとおり、右端固定のピボットでは入力がソート済みの場合に O(n²) になるリスクがありますね。実運用向きではない気はします。
private int partition(List<Integer> nums, int left, int right, Map<Integer, Integer> numToCount) { | ||
// Using the rightmost element as pivot | ||
int pivotFrequency = numToCount.get(nums.get(right)); | ||
int i = left; |
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.
個人的には、iの生存期間はある程度長いし重要な変数なので、もうちょっとしっかりした変数名をつけたい気持ちになります。
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.
たしかに重要な変数の割に通例のインデックスと同じ命名にするのはよくないかもですね。pivotIndexとかもう少ししっかりした変数名にしようと思いました。
問題
https://leetcode.com/problems/top-k-frequent-elements/
言語
Java
次に解く問題
Find K Pairs with Smallest Sums