-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
347. Top K Frequent Elements update.
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
08-Heap-and-Priority-Queue/347-Top-K-Frequent-Elements/src/Solution3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import java.util.*; | ||
|
||
/** | ||
* @Auther: lss | ||
* @Date: 2019/2/26 19:00 | ||
* @Description: | ||
*/ | ||
public class Solution3 { | ||
|
||
public List<Integer> topKFrequent(int[] nums, int k) { | ||
|
||
TreeMap<Integer, Integer> map = new TreeMap<>(); | ||
for (int num : nums) { | ||
if (map.containsKey(num)) | ||
map.put(num, map.get(num) + 1); | ||
else | ||
map.put(num, 1); | ||
} | ||
|
||
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() { | ||
@Override | ||
public int compare(Integer a, Integer b) { | ||
return map.get(a) - map.get(b); | ||
} | ||
}); | ||
for (int key : map.keySet()) { | ||
if (pq.size() < k) | ||
pq.add(key); | ||
else if (map.get(key) > map.get(pq.peek())) { | ||
pq.remove(); | ||
pq.add(key); | ||
} | ||
} | ||
|
||
LinkedList<Integer> res = new LinkedList<>(); | ||
while (!pq.isEmpty()) | ||
res.add(pq.remove()); | ||
return res; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
08-Heap-and-Priority-Queue/347-Top-K-Frequent-Elements/src/Solution4.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import java.util.*; | ||
|
||
/** | ||
* @Auther: lss | ||
* @Date: 2019/2/26 19:00 | ||
* @Description: | ||
*/ | ||
public class Solution4 { | ||
|
||
public List<Integer> topKFrequent(int[] nums, int k) { | ||
|
||
TreeMap<Integer, Integer> map = new TreeMap<>(); | ||
for (int num : nums) { | ||
if (map.containsKey(num)) | ||
map.put(num, map.get(num) + 1); | ||
else | ||
map.put(num, 1); | ||
} | ||
|
||
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> map.get(a) - map.get(b)); | ||
for (int key : map.keySet()) { | ||
if (pq.size() < k) | ||
pq.add(key); | ||
else if (map.get(key) > map.get(pq.peek())) { | ||
pq.remove(); | ||
pq.add(key); | ||
} | ||
} | ||
|
||
LinkedList<Integer> res = new LinkedList<>(); | ||
while (!pq.isEmpty()) | ||
res.add(pq.remove()); | ||
return res; | ||
} | ||
} |