Skip to content

Commit c27fea3

Browse files
committed
Add the Top K-Frequent Elements problem under the Sorting section
1 parent 41deebe commit c27fea3

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

src/sorting/QuickKthSelect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Solution Link: https://leetcode.com/problems/kth-largest-element-in-an-array/solutions/60300/java-quick-select/
77

88
// Approach: Use Quick Sorting with the Binary Search Technique
9-
// Average Time Complexity: O(N); Worst Time Complexity: O(N^2)
9+
// Average Time Complexity: O(N), Worst Time Complexity: O(N^2)
1010

1111
public class QuickKthSelect {
1212

src/sorting/QuickSortArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Problem Link: https://leetcode.com/problems/sort-an-array/
66
// Solution Link: https://leetcode.com/problems/sort-an-array/solutions/492042/7-sorting-algorithms-quick-sort-top-down-bottom-up-merge-sort-heap-sort-etc/
77

8-
// Average Time Complexity: O(NlogN); Worst Time Complexity: O(N^2); Worst Space Complexity: O(logN)
8+
// Average Time Complexity: O(NlogN), Worst Time Complexity: O(N^2); Worst Space Complexity: O(logN)
99

1010
public class QuickSortArray {
1111

src/sorting/TopKFrequents.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package sorting;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
// Problem Link: https://leetcode.com/problems/top-k-frequent-elements/
9+
// Solution Link: https://www.youtube.com/watch?v=YPTqKIgVk-k
10+
11+
// Approach: Bucket Sort Technique; Time Complexity: O(N), Space Complexity: O(N)
12+
13+
public class TopKFrequents {
14+
15+
public int[] compute(int[] nums, int k) {
16+
17+
@SuppressWarnings("unchecked")
18+
List<Integer>[] buckets = new LinkedList[nums.length + 1];
19+
Map<Integer, Integer> freqs = new HashMap<>();
20+
21+
int[] tops = new int[k];
22+
23+
for (int num : nums) {
24+
25+
int freq = freqs.getOrDefault(num, 0);
26+
27+
freqs.put(num, freq + 1);
28+
}
29+
30+
for (int num : freqs.keySet()) {
31+
32+
int freq = freqs.get(num);
33+
34+
if (buckets[freq] == null) {
35+
36+
buckets[freq] = new LinkedList<>();
37+
}
38+
39+
buckets[freq].add(num);
40+
}
41+
42+
int index = k, freq = buckets.length;
43+
44+
while (--freq > 0 && k > 0) {
45+
46+
if (buckets[freq] != null) {
47+
48+
for (int num : buckets[freq]) {
49+
50+
tops[index - k] = num;
51+
52+
if (--k == 0) break;
53+
}
54+
}
55+
}
56+
57+
return tops;
58+
}
59+
}

src/sorting/UnsortedSubArray.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// find their proper Indices in the Sorted whole-array; Time Complexity: O(N)
99

1010
public class UnsortedSubArray {
11-
11+
1212
public int compute(int[] nums) {
1313

1414
int last = nums.length - 1;
@@ -37,7 +37,7 @@ public int compute(int[] nums) {
3737
// = the unsorted sub-array AT LEAST needs to begin here
3838
if (nums[last - index] > low) left = last - index;
3939
}
40-
40+
4141
return right - left + 1;
4242
}
4343
}

0 commit comments

Comments
 (0)