Skip to content

Commit 7af9a0b

Browse files
committed
fd
1 parent fd17ed9 commit 7af9a0b

File tree

3 files changed

+73
-31
lines changed

3 files changed

+73
-31
lines changed

solution/src/main/java/com/inuker/solution/TopKFrequentElements.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,44 @@
1515

1616
public class TopKFrequentElements {
1717

18-
// 耗时46ms,最差复杂度O(nlgn)
18+
/**
19+
* 先统计每个元素次数,再用Priority排序
20+
*/
21+
// 耗时46ms,最差复杂度O(nlgn),当k<<n时为O(n)
1922
public List<Integer> topKFrequent(int[] nums, int k) {
20-
Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
23+
HashMap<Integer, Integer> map = new HashMap<>();
2124
for (int n : nums) {
2225
map.put(n, map.getOrDefault(n, 0) + 1);
2326
}
24-
Queue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
27+
Queue<Integer> queue = new PriorityQueue<Integer>(8, new Comparator<Integer>() {
2528
@Override
26-
public int compare(int[] o1, int[] o2) {
27-
return o2[1] - o1[1];
29+
public int compare(Integer o1, Integer o2) {
30+
return map.get(o1) - map.get(o2);
2831
}
2932
});
30-
for (int key : map.keySet()) {
31-
queue.add(new int[] { key, map.get(key) });
33+
for (Integer n : map.keySet()) {
34+
queue.offer(n);
35+
if (queue.size() > k) {
36+
queue.poll();
37+
}
3238
}
33-
List<Integer> list = new LinkedList<Integer>();
34-
for (int i = 1; i <= k && !queue.isEmpty(); i++) {
35-
list.add(queue.poll()[0]);
39+
List<Integer> list = new LinkedList<>();
40+
while (!queue.isEmpty()) {
41+
list.add(queue.poll());
3642
}
3743
return list;
3844
}
3945

40-
// 耗时23ms,复杂度O(n)
46+
// 耗时23ms,时间复杂度O(n),空间复杂度O(n)
4147
public List<Integer> topKFrequent2(int[] nums, int k) {
4248
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
49+
int max = 0;
4350
for (int n : nums) {
44-
map.put(n, map.getOrDefault(n, 0) + 1);
51+
int count = map.getOrDefault(n, 0) + 1;
52+
map.put(n, count);
53+
max = Math.max(max, count);
4554
}
46-
List<Integer>[] lists = new LinkedList[nums.length + 1];
55+
List<Integer>[] lists = new LinkedList[max + 1];
4756
for (int key : map.keySet()) {
4857
int count = map.get(key);
4958
if (lists[count] == null) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.inuker.test;
2+
3+
/**
4+
* Created by liwentian on 2017/12/4.
5+
*/
6+
7+
public class Test {
8+
9+
Caller mCaller;
10+
11+
void call(String name) {
12+
if (mCaller == null) {
13+
mCaller = new Caller() {
14+
@Override
15+
public void call() {
16+
System.out.println(name);
17+
}
18+
};
19+
}
20+
mCaller.call();
21+
}
22+
23+
interface Caller {
24+
void call();
25+
}
26+
}
Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.inuker.test;
22

3+
import com.inuker.solution.NestedInteger;
34
import com.leetcode.library.Interval;
45
import com.leetcode.library.TreeNode;
56

@@ -16,34 +17,40 @@
1617
import java.util.List;
1718
import java.util.PriorityQueue;
1819
import java.util.Queue;
20+
import java.util.Random;
1921
import java.util.Set;
2022
import java.util.Stack;
23+
import java.util.TreeMap;
2124

2225
public class main {
2326

2427
public static void main(String[] args) {
25-
28+
Test test = new Test();
29+
test.call("hello");
30+
test.call("how");
2631
}
2732

28-
class Logger {
29-
30-
HashMap<String, Integer> map = new HashMap<>();
31-
32-
/** Initialize your data structure here. */
33-
public Logger() {
34-
33+
public List<Integer> topKFrequent(int[] nums, int k) {
34+
HashMap<Integer, Integer> map = new HashMap<>();
35+
for (int n : nums) {
36+
map.put(n, map.getOrDefault(n, 0) + 1);
3537
}
36-
37-
/** Returns true if the message should be printed in the given timestamp, otherwise returns false.
38-
If this method returns false, the message will not be printed.
39-
The timestamp is in seconds granularity. */
40-
public boolean shouldPrintMessage(int timestamp, String message) {
41-
int time = map.getOrDefault(message, -100);
42-
if (time <= timestamp - 10) {
43-
map.put(message, timestamp);
44-
return true;
38+
Queue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
39+
@Override
40+
public int compare(Integer o1, Integer o2) {
41+
return map.get(o1) - map.get(o2);
42+
}
43+
});
44+
for (Integer n : map.keySet()) {
45+
queue.add(n);
46+
if (queue.size() > k) {
47+
queue.poll();
4548
}
46-
return false;
4749
}
50+
List<Integer> list = new LinkedList<>();
51+
while (!queue.isEmpty()) {
52+
list.add(queue.poll());
53+
}
54+
return list;
4855
}
4956
}

0 commit comments

Comments
 (0)