-
Notifications
You must be signed in to change notification settings - Fork 5
/
LC_295_FindMediaDataStream.cpp
169 lines (141 loc) · 3.27 KB
/
LC_295_FindMediaDataStream.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
https://practice.geeksforgeeks.org/problems/find-median-in-a-stream-1587115620/1#.
https://leetcode.com/problems/find-median-from-data-stream/
295. Find Median from Data Stream
*/
//LC
class MedianFinder {
public:
priority_queue<int> mxh;
priority_queue<int, vector<int>, greater<int>> mih;
MedianFinder() {
}
void addNum(int x) {
if(mxh.empty() || x < mxh.top())
mxh.push(x);
else
mih.push(x);
// balance
if(mxh.size() == mih.size()+2)
{
mih.push(mxh.top());
mxh.pop();
}
else if(mxh.size()+2 == mih.size())
{
mxh.push(mih.top());
mih.pop();
}
}// add num
double findMedian() {
if(mxh.size() == mih.size())
return (mxh.top()+mih.top())/2.0;
else if(mxh.size() > mih.size())
return mxh.top();
else
return mih.top();
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/
// GFG
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
priority_queue<int> mxh;
priority_queue<int, vector<int>, greater<int>> mih;
//Function to insert heap.
void insertHeap(int &x)
{
// USing insertion sort
// arr.push_back(x);
// return insertionSort(x);
if(mxh.empty() || x < mxh.top())
mxh.push(x);
else
mih.push(x);
balanceHeaps();
}// insert Heap
//Function to balance heaps.
void balanceHeaps()
{
if(mxh.size() == mih.size()+2)
{
mih.push(mxh.top());
mxh.pop();
}
else if(mxh.size()+2 == mih.size())
{
mxh.push(mih.top());
mih.pop();
}
}
//Function to return Median.
double getMedian()
{
if(mxh.size() == mih.size())
return (mxh.top()+mih.top())/2;
else if(mxh.size() > mih.size())
return mxh.top();
else
return mih.top();
}
// USING INSERTION SORT ==> TLE
vector<int> arr; //
void insertionSort(int &x)
{
int n=arr.size()-1;
int new_ele = arr[n];
int j = n-1;
while(j>=0 && new_ele < arr[j])
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = new_ele;
}// insertionSort
double getMedianInsertionSort()
{
// USING INSERTION SORT
int n = arr.size();
if(n==1) return arr[0];
else
{
if(n % 2)
{
return arr[n/2];
}
else
{
return (arr[n/2-1]+arr[n/2])/2;
}
}
}
};
// { Driver Code Starts.
int main()
{
int n, x;
int t;
cin>>t;
while(t--)
{
Solution ob;
cin >> n;
for(int i = 1;i<= n; ++i)
{
cin >> x;
ob.insertHeap(x);
cout << floor(ob.getMedian()) << endl;
}
}
return 0;
} // } Driver Code Ends