Skip to content

Commit 660e5f1

Browse files
committed
692_Top_K_Frequent_Words
1 parent adbe57f commit 660e5f1

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ Also, there are open source implementations for basic data structs and algorithm
152152
| 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/subtree-of-another-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/581_Shortest_Unsorted_Continuous_Subarray.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/581_Shortest_Unsorted_Continuous_Subarray.java) | 1. Sort and find the difference (min and max), O(nlgn)<br>2. Using stack to find boundaries (push when correct order, pop when not correct), O(n) and O(n)<br>3. Find min and max of unordered array, O(n) and O(1)|
153153
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/617_Merge_Two_Binary_Trees.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/617_Merge_Two_Binary_Trees.java) | Traverse both trees Recursion & Iterative (stack) |
154154
| 654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/654_Maximum_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/654_Maximum_Binary_Tree.java) | 1. Divide and conquer, recursive, O(n^2)<br>2. Monotonic stack, O(n) |
155+
| 692 | [Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/692_Top_K_Frequent_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/692_Top_K_Frequent_Words.java) | 1. Sort based on frequency and alphabetical order, O(nlgn) and O(n)<br>2. Find top k with Heap, O(nlogk) and O(n) |
155156
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/697_Degree_of_an_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/697_Degree_of_an_Array.java) | 1. Find degree and value, then find smallest subarray (start and end with this value), O(n) and O(n)<br>2. Go through nums, remember left most pos and right most for each value, O(n) and O(n) |
156-
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/703_Kth_Largest_Element_in_a_Stream.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/703_Kth_Largest_Element_in_a_Stream.java) | 1. Sort and insert into right place, O(nlgn) and O(n)<br>2. k largest heap, O(klgn) and O(n) |
157+
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/703_Kth_Largest_Element_in_a_Stream.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/703_Kth_Largest_Element_in_a_Stream.java) | 1. Sort and insert into right place, O(nlgn) and O(n)<br>2. k largest heap, O(nlogk) and O(n) |
157158
| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/706_Design_HashMap.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/706_Design_HashMap.java) | Hash implementation, mod is fine. Be careful about key conflict and key remove. |
158159
| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/709_To_Lower_Case.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/709_To_Lower_Case.java) | String processing:<br>1. str.lower() or str.toLowerCase()<br>2. ASCII processing. O(n) and O(1) |
159160
| 716 | [Max Stack](https://leetcode.com/problems/max-stack/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/716_Max_Stack.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/716_Max_Stack.java) | 1. Two stacks<br> 2. Double linked list and Hash |

java/692_Top_K_Frequent_Words.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
/*public List<String> topKFrequent(String[] words, int k) {
3+
Map<String, Integer> count = new HashMap();
4+
for (String word: words) {
5+
count.put(word, count.getOrDefault(word, 0) + 1);
6+
}
7+
List<String> candidates = new ArrayList(count.keySet());
8+
Collections.sort(candidates, (w1, w2) -> count.get(w1).equals(count.get(w2)) ?
9+
w1.compareTo(w2) : count.get(w2) - count.get(w1));
10+
11+
return candidates.subList(0, k);
12+
}*/
13+
public List<String> topKFrequent(String[] words, int k) {
14+
Map<String, Integer> count = new HashMap();
15+
for (String word: words) {
16+
count.put(word, count.getOrDefault(word, 0) + 1);
17+
}
18+
PriorityQueue<String> heap = new PriorityQueue<String>(
19+
(w1, w2) -> count.get(w1).equals(count.get(w2)) ?
20+
w2.compareTo(w1) : count.get(w1) - count.get(w2) );
21+
22+
for (String word: count.keySet()) {
23+
heap.offer(word);
24+
if (heap.size() > k) heap.poll();
25+
}
26+
27+
List<String> ans = new ArrayList();
28+
while (!heap.isEmpty()) ans.add(heap.poll());
29+
Collections.reverse(ans);
30+
return ans;
31+
}
32+
}

python/692_Top_K_Frequent_Words.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
# def topKFrequent(self, words, k):
3+
# """
4+
# :type words: List[str]
5+
# :type k: int
6+
# :rtype: List[str]
7+
# """
8+
# counter = collections.Counter(words)
9+
# res = sorted(counter.items(), cmp=cmp_frequency, reverse=True)
10+
# return [k for k, _ in res[:k]]
11+
12+
# def cmp_frequency(x, y):
13+
# if x[1] != y[1]:
14+
# return cmp(x[1], y[1])
15+
# return cmp(y[0], x[0])
16+
17+
# def topKFrequent(self, words, k):
18+
# count = collections.Counter(words)
19+
# candidates = count.keys()
20+
# candidates.sort(key = lambda w: (-count[w], w))
21+
# return candidates[:k]
22+
23+
def topKFrequent(self, words, k):
24+
count = collections.Counter(words)
25+
# Note that python heapq only support min heap
26+
# So, we can make the value negative to create a max heap
27+
heap = [(-freq, word) for word, freq in count.items()]
28+
heapq.heapify(heap)
29+
return [heapq.heappop(heap)[1] for _ in xrange(k)]

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