Conversation
| } | ||
|
|
||
| int idx = 0; | ||
| for (int i = bucket.length - 1; i >= 0 && idx < k; i--) { |
There was a problem hiding this comment.
idxがkになったら、breakされるように書かれているので、idx < kはなくても良いですかね。
| frequency.put(num, frequency.getOrDefault(num, 0) + 1); | ||
| } | ||
|
|
||
| PriorityQueue<Map.Entry<Integer, Integer>> minHeap = new PriorityQueue<>((a, b) -> a.getValue() - b.getValue()); |
There was a problem hiding this comment.
データ構造ではなく、実際に入っている中身で変数名を決めると良いかもです。
私ならk_topsと名付けます。
There was a problem hiding this comment.
ありがとうございます。
そうですね、取り込みます。
| for (int key : frequency.keySet()) { | ||
| int value = frequency.get(key); |
There was a problem hiding this comment.
keyはnum, valueはfrequencyで良いかもです。
かぶってしまうので、元のfrequencyはnumTo Frequencyとするのはどうでしょう。
map系で、keyToValueという命名の仕方をしている人は多い印象です。
There was a problem hiding this comment.
ありがとうございます。
そっちの方がいいですね、取り込みます。
| class Solution { | ||
| public int[] topKFrequent(int[] nums, int k) { | ||
| int[] result = new int[k]; | ||
| HashMap<Integer, Integer> frequency = new HashMap<>(); |
There was a problem hiding this comment.
型を Map<Integer, Integer> とするのをよく見かけます。このあたりは所属するチームの平均的な書き方に合わせることをお勧めいたします。
| ```java | ||
| class Solution { | ||
| public int[] topKFrequent(int[] nums, int k) { | ||
| int[] result = new int[k]; |
There was a problem hiding this comment.
変数を使用箇所よりだいぶ前で定義をすると、読み手は使用箇所まで変数の定義を覚えていなければならず、短期記憶を圧迫してしまいます。変数は使用箇所の直前で定義することをお勧めいたします。
| bucket[value].add(key); | ||
| } | ||
|
|
||
| int idx = 0; |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)
347. Top K Frequent Elementsを解きました。
レビューお願いします。