Skip to content

Commit c5ecfe5

Browse files
refactor 347
1 parent dbe3e86 commit c5ecfe5

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ Your ideas/fixes/algorithms are more than welcome!
361361
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_350.java)| O(m+n)|O((m+n)) could be optimized | |Easy| HashMap, Binary Search
362362
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_349.java)| O(m+n)|O(min(m,n)) | |Easy| Two Pointers, Binary Search
363363
|348|[Design Tic-Tac-Toe](https://leetcode.com/problems/design-tic-tac-toe/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_348.java)| O(1)|O(n) | |Medium| Design
364-
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_347.java)| O(n)|O(1) | |Medium| HashTable, Heap, Bucket Sort
364+
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_347.java)| O(n)|O(k) k is is the number of unique elements in the given array | |Medium| HashTable, Heap, Bucket Sort
365365
|346|[Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_346.java)| O(1)|O(w)) | |Easy| Queue
366366
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_345.java) | O(n) |O(1) | |Easy | String
367367
|344|[Reverse String](https://leetcode.com/problems/reverse-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_344.java) | O(n) |O(1) | |Easy | String

src/main/java/com/fishercoder/solutions/_347.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ public List<Integer> topKFrequent(int[] nums, int k) {
6767

6868
// build heap, this is O(logn)
6969
Queue<Entry<Integer, Integer>> heap = new PriorityQueue<>((o1, o2) -> {
70-
if (o1.getValue() > o2.getValue()) {
71-
return -1;
72-
} else if (o1.getValue() < o2.getValue()) {
73-
return 1;
74-
} else {
75-
return 0;
76-
}
77-
});
70+
if (o1.getValue() > o2.getValue()) {
71+
return -1;
72+
} else if (o1.getValue() < o2.getValue()) {
73+
return 1;
74+
} else {
75+
return 0;
76+
}
77+
});
7878
for (Entry<Integer, Integer> entry : map.entrySet()) {
7979
heap.offer(entry);
8080
}
@@ -86,5 +86,4 @@ public List<Integer> topKFrequent(int[] nums, int k) {
8686
return res;
8787
}
8888
}
89-
9089
}

src/test/java/com/fishercoder/_347Test.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ public void test1() {
2828
expected = new ArrayList<>(Arrays.asList(0, 3));
2929
/**Comment out until Leetcode addresses this test case:
3030
* https://discuss.leetcode.com/topic/44237/java-o-n-solution-bucket-sort/75
31-
* Then I'll update this Solution1 code accordingly.*/
32-
// assertEquals(expected, solution1.topKFrequent(nums, 2));
31+
* Then I'll update this Solution1 code accordingly.
32+
*
33+
* My post is still un-addressed. - 3/12/2018*/
34+
//assertEquals(expected, solution1.topKFrequent(nums, 2));
3335
}
3436

3537
@Test
@@ -38,4 +40,11 @@ public void test2() {
3840
expected = new ArrayList<>(Arrays.asList(0, 3));
3941
assertEquals(expected, solution2.topKFrequent(nums, 2));
4042
}
43+
44+
@Test
45+
public void test3() {
46+
nums = new int[] {1, 1, 1, 2, 2, 3};
47+
expected = new ArrayList<>(Arrays.asList(1, 2));
48+
assertEquals(expected, solution1.topKFrequent(nums, 2));
49+
}
4150
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy