-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay35.java
More file actions
80 lines (62 loc) · 1.96 KB
/
Copy pathDay35.java
File metadata and controls
80 lines (62 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*Kth Smallest
Given an integer array arr[] and an integer k, your task is to find and return the kth smallest element in the given array.
Note: The kth smallest element is determined based on the sorted order of the array.
Examples :
Input: arr[] = [10, 5, 4, 3, 48, 6, 2, 33, 53, 10], k = 4
Output: 5
Explanation: 4th smallest element in the given array is 5.
Input: arr[] = [7, 10, 4, 3, 20, 15], k = 3
Output: 7
Explanation: 3rd smallest element in the given array is 7. */
/*import java.util.PriorityQueue;
class Solution {
public static int kthSmallest(int[] arr, int k) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for (int num : arr) {
minHeap.add(num);
}
// Remove first k-1 smallest elements
for (int i = 1; i < k; i++) {
minHeap.poll();
}
return minHeap.peek();
}
}
*/
/*⏱️ Complexity
Time: O(n + k log n)
Space: O(n) */
/* class Solution {
public static int kthSmallest(int[] arr, int k) {
return quickSelect(arr, 0, arr.length - 1, k - 1);
}
static int quickSelect(int[] arr, int low, int high, int k) {
int pivotIndex = partition(arr, low, high);
if (pivotIndex == k)
return arr[pivotIndex];
else if (pivotIndex > k)
return quickSelect(arr, low, pivotIndex - 1, k);
else
return quickSelect(arr, pivotIndex + 1, high, k);
}
static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
int temp = arr[i];
arr[i] = arr[high];
arr[high] = temp;
return i;
}
} */
/* ⏱️ Complexity
Average Time: O(n)
Worst Case: O(n²) (rare, can be improved using random pivot)
Space: O(1)*/