-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_215_KthLargestEleInArray.cpp
123 lines (109 loc) · 3.26 KB
/
LC_215_KthLargestEleInArray.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
https://leetcode.com/problems/kth-largest-element-in-an-array/
215. Kth Largest Element in an Array
*/
class Solution {
public:
class minHeap
{
vector<int> arr;
public:
minHeap(vector<int>& v, int size)
{
arr = v;
// build heap
for(int i=(size/2)-1; i>=0; i--)
heapify_down(i);
}
int parent(int i){ return (i-1)/2; }
int left (int i) { return (2*i +1);}
int right(int i){ return (2*i + 2);}
int getMinRoot(){ return arr.at(0);}
void heapify_down(int root)
{
int l = left(root);
int r = right(root);
int smallest = root;
if(l<arr.size() && arr[l] < arr[smallest])
smallest = l;
if(r<arr.size() && arr[r] < arr[smallest])
smallest = r;
if(smallest !=root)
{
swap(arr[root], arr[smallest]);
heapify_down(smallest);
}
}
int extractMin()
{
if(arr.size() ==0)return -1;
int top = arr[0];
if(arr.size()>1)
{
arr[0] = arr[arr.size()-1];
heapify_down(0);
}
arr.resize(arr.size()-1);
return top;
}
};
int findKthLargest(vector<int>& nums, int k) {
// Using self minHeap
minHeap mh(nums,nums.size());
for(int i=0; i<nums.size()-k; i++)
{
mh.extractMin();
}
return mh.getMinRoot();
}
/*
// Using Sorting
sort(nums.begin(), nums.end());
return nums[nums.size()-k];
*/
/*
// Using MIN Heap for largest
// Push First K elements into the heap
priority_queue<int, vector<int>, greater<int>> pq(nums.begin(), nums.begin()+k);
// for the rest of the elements insert only if it is bigger.
for(int i=k; i<nums.size(); i++)
{
if(pq.top() < nums[i])
{
pq.pop();
pq.push(nums[i]);
}
}
return pq.top();
*/
/*
// Using QuickSort/ Select
int n = nums.size();
int l=0, r=nums.size()-1;
k = n-k; // largest to smallest element 6-2=4(0-1-2-3-4-5)
while(l<r)
{
int pos = partition(nums,l,r);
if(pos==k) break; // if position is same as kth element
if(pos < k) l=pos+1; // go to left side towards kth element
else r = pos-1;
}
return nums[k];
}
int partition(vector<int>& nums, int low, int high)
{
int X = nums[low]; //first element as pivot;
int j = low; // pivot index
for(int i=low+1; i<=high; i++)
{
if(nums[i]<X)
{
j++;
swap(nums[i], nums[j]);
}
}
swap(nums[j], nums[low]);
return j;
}
*/
};