Skip to content

Commit 303f505

Browse files
committed
347_Top_K_Frequent_Elements
1 parent b0cc071 commit 303f505

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Also, there are open source implementations for basic data structs and algorithm
112112
| 339 | [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/339_Nested_List_Weight_Sum.py) | Depth-first recursion |
113113
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py) | Maintain a sliding window with at most k distinct characters and a count for this window. Note that the start position need a loop to update.|
114114
| 346 | [Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/346_Moving_Average_from_Data_Stream.py) | fix-sized queue or dequeue, O(1) and O(n) |
115+
| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/347_Top_K_Frequent_Elements.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/347_Top_K_Frequent_Elements.java) | Sort by frequency, O(nlogn) and O(n). |
115116
| 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/351_Android_Unlock_Patterns.py) | Backtracking, O(n!) and O(n) |
116117
| 359 | [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/359_Logger_Rate_Limiter.py) | 1. hash which stores the latest timestamp, O(1) and O(n)<br>2. Using Priority queue to remove older logs, O(n) and O(n) |
117118
| 366 | [Find Leaves of Binary Tree](https://leetcode.com/problems/find-leaves-of-binary-tree/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/366_Find_Leaves_of_Binary_Tree.py) | 1. Set or hash to check leaft, O(n^2) and O(n)<br>2. Recursively check level and return them, O(n) and O(n)|
@@ -151,7 +152,7 @@ Also, there are open source implementations for basic data structs and algorithm
151152
| 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) |
152153
| 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) |
153154
| 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) |
154-
| 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. |
155+
| 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. |
155156
| 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) |
156157
| 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 |
157158
| 720 | [Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/720_Longest_Word_in_Dictionary.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/720_Longest_Word_in_Dictionary.java) | 1. Brute Force, O(sum(w^2)) and O(w)<br>2. Tire Tree, O(sum(w) and O(w))<br>3. Sort and word without last char, O(nlogn + sum(w)) and O(w) |

java/347_Top_K_Frequent_Elements.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public List<Integer> topKFrequent(int[] nums, int k) {
3+
// build hash map : character and how often it appears
4+
HashMap<Integer, Integer> count = new HashMap();
5+
for (int n: nums) {
6+
count.put(n, count.getOrDefault(n, 0) + 1);
7+
}
8+
9+
// init heap 'the less frequent element first'
10+
PriorityQueue<Integer> heap =
11+
new PriorityQueue<Integer>((n1, n2) -> count.get(n1) - count.get(n2));
12+
13+
// keep k top frequent elements in the heap
14+
for (int n: count.keySet()) {
15+
heap.add(n);
16+
if (heap.size() > k)
17+
heap.poll();
18+
}
19+
20+
// build output list
21+
List<Integer> top_k = new LinkedList();
22+
while (!heap.isEmpty())
23+
top_k.add(heap.poll());
24+
Collections.reverse(top_k);
25+
return top_k;
26+
}
27+
}

python/347_Top_K_Frequent_Elements.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution(object):
2+
def topKFrequent(self, nums, k):
3+
"""
4+
:type nums: List[int]
5+
:type k: int
6+
:rtype: List[int]
7+
"""
8+
counter = collections.Counter(nums)
9+
return [k for k,v in counter.most_common(k)]
10+
# return heapq.nlargest(k, count.keys(), key=count.get)

python/374_Guess_Number_Higher_or_Lower.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ def guessNumber(self, n):
1919
elif res > 0:
2020
begin = mid + 1
2121
else:
22-
end = mid - 1
22+
end = mid - 1

0 commit comments

Comments
 (0)