Skip to content

Commit 418bfd2

Browse files
committed
fd
1 parent 58ec94d commit 418bfd2

File tree

2 files changed

+52
-74
lines changed

2 files changed

+52
-74
lines changed

leetcode/solution/src/TopKFrequentElements.java

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,39 @@
33
public class TopKFrequentElements {
44

55
/**
6-
* 先统计每个元素次数,再用Priority排序
6+
* 首选方法,时间复杂度O(n),省去了排序
7+
* 结尾注意list的size大于k了,要截一部分
78
*/
8-
// 耗时46ms,最差复杂度O(nlgn),当k<<n时为O(n)
99
public List<Integer> topKFrequent(int[] nums, int k) {
10+
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
11+
int max = 0;
12+
for (int n : nums) {
13+
int count = map.getOrDefault(n, 0) + 1;
14+
map.put(n, count);
15+
max = Math.max(max, count);
16+
}
17+
List<Integer>[] lists = new LinkedList[max + 1];
18+
for (int key : map.keySet()) {
19+
int count = map.get(key);
20+
if (lists[count] == null) {
21+
lists[count] = new LinkedList<Integer>();
22+
}
23+
lists[count].add(key);
24+
}
25+
List<Integer> result = new LinkedList<Integer>();
26+
for (int i = lists.length - 1; i >= 0 && result.size() < k; i--) {
27+
if (lists[i] != null) {
28+
result.addAll(lists[i]);
29+
}
30+
}
31+
return result.subList(0, k);
32+
}
33+
34+
/**
35+
* 先统计每个元素次数,再用Priority排序
36+
*/
37+
// 耗时46ms,最差复杂度O(nlgk),当k<<n时为O(n)
38+
public List<Integer> topKFrequent2(int[] nums, int k) {
1039
HashMap<Integer, Integer> map = new HashMap<>();
1140
for (int n : nums) {
1241
map.put(n, map.getOrDefault(n, 0) + 1);
@@ -30,7 +59,7 @@ public int compare(Integer o1, Integer o2) {
3059
return list;
3160
}
3261

33-
public List<Integer> topKFrequent2(int[] nums, int k) {
62+
public List<Integer> topKFrequent3(int[] nums, int k) {
3463
Map<Integer, Integer> map = new HashMap<>();
3564
for(int n: nums){
3665
map.put(n, map.getOrDefault(n,0)+1);
@@ -52,31 +81,4 @@ public List<Integer> topKFrequent2(int[] nums, int k) {
5281
}
5382
return res;
5483
}
55-
56-
// 耗时23ms,时间复杂度O(n),空间复杂度O(n)
57-
// 这里有个问题,result的size可能大于k了
58-
public List<Integer> topKFrequent3(int[] nums, int k) {
59-
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
60-
int max = 0;
61-
for (int n : nums) {
62-
int count = map.getOrDefault(n, 0) + 1;
63-
map.put(n, count);
64-
max = Math.max(max, count);
65-
}
66-
List<Integer>[] lists = new LinkedList[max + 1];
67-
for (int key : map.keySet()) {
68-
int count = map.get(key);
69-
if (lists[count] == null) {
70-
lists[count] = new LinkedList<Integer>();
71-
}
72-
lists[count].add(key);
73-
}
74-
List<Integer> result = new LinkedList<Integer>();
75-
for (int i = lists.length - 1; i >= 0 && result.size() < k; i--) {
76-
if (lists[i] != null) {
77-
result.addAll(lists[i]);
78-
}
79-
}
80-
return result.subList(0, k);
81-
}
8284
}

leetcode/src/Main.java

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,34 @@ public class Main {
44

55
public static class Solution {
66

7-
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
8-
HashSet<String> words = new HashSet<>(wordList);
9-
Queue<String> queue = new LinkedList<>();
10-
queue.offer(beginWord);
11-
12-
Queue<String> next = new LinkedList<>();
13-
int length = 1;
14-
15-
while (!queue.isEmpty()) {
16-
String s = queue.poll();
17-
18-
if (s.equals(endWord)) {
19-
return length;
20-
}
21-
22-
if (!words.isEmpty()) {
23-
StringBuilder sb = new StringBuilder(s);
24-
for (int i = 0; i < s.length(); i++) {
25-
for (char c = 'a'; c <= 'z'; c++) {
26-
if (c == s.charAt(i)) {
27-
continue;
28-
}
29-
30-
sb.setCharAt(i, c);
31-
String st = sb.toString();
32-
33-
if (words.contains(st)) {
34-
next.offer(st);
35-
words.remove(st);
36-
}
37-
}
38-
sb.setCharAt(i, s.charAt(i));
39-
}
7+
public List<Integer> topKFrequent(int[] nums, int k) {
8+
HashMap<Integer, Integer> map = new HashMap<>();
9+
int max = 0;
10+
for (int n : nums) {
11+
int cnt = map.getOrDefault(n, 0) + 1;
12+
map.put(n, cnt);
13+
max = Math.max(max, cnt);
14+
}
15+
List<Integer>[] lists = new ArrayList[max + 1];
16+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
17+
int freq = entry.getValue();
18+
if (lists[freq] == null) {
19+
lists[freq] = new ArrayList<>();
4020
}
41-
42-
if (queue.isEmpty()) {
43-
queue.addAll(next);
44-
next.clear();
45-
length++;
21+
lists[freq].add(entry.getKey());
22+
}
23+
List<Integer> result = new ArrayList<>();
24+
for (int i = max; i >= 0 && result.size() <= k; i--) {
25+
if (lists[i] != null) {
26+
result.addAll(lists[i]);
4627
}
4728
}
48-
49-
return 0;
29+
return result.subList(0, k);
5030
}
5131
}
5232

5333

5434
public static void main(String[] args) {
5535
Solution solution = new Solution();
56-
int len = solution.ladderLength("hit", "cog", Arrays.asList(new String[]{
57-
"hot", "dot", "dog", "lot", "log"
58-
}));
59-
System.out.println(len);
6036
}
6137
}

0 commit comments

Comments
 (0)