File tree Expand file tree Collapse file tree 4 files changed +63
-4
lines changed Expand file tree Collapse file tree 4 files changed +63
-4
lines changed Original file line number Diff line number Diff line change 6
6
// Solution Link: https://leetcode.com/problems/kth-largest-element-in-an-array/solutions/60300/java-quick-select/
7
7
8
8
// 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)
10
10
11
11
public class QuickKthSelect {
12
12
Original file line number Diff line number Diff line change 5
5
// Problem Link: https://leetcode.com/problems/sort-an-array/
6
6
// 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/
7
7
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)
9
9
10
10
public class QuickSortArray {
11
11
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 8
8
// find their proper Indices in the Sorted whole-array; Time Complexity: O(N)
9
9
10
10
public class UnsortedSubArray {
11
-
11
+
12
12
public int compute (int [] nums ) {
13
13
14
14
int last = nums .length - 1 ;
@@ -37,7 +37,7 @@ public int compute(int[] nums) {
37
37
// = the unsorted sub-array AT LEAST needs to begin here
38
38
if (nums [last - index ] > low ) left = last - index ;
39
39
}
40
-
40
+
41
41
return right - left + 1 ;
42
42
}
43
43
}
You can’t perform that action at this time.
0 commit comments