here is the link to my code
can you please help to reduce time complexity of this solution as one test case is causing TLE error
Hostel visits problem
instead of first copying data to temp
and then poping k elements
you can use max heap
look at this simple and efficient approach
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main () {
int n, k; cin >> n >> k;
priority_queue <int> pq;
for (int i = 0; i < n; i++) {
int qtype; cin >> qtype;
if (qtype == 1) {
int x, y; cin >> x >> y;
pq.push(x*x + y*y);
} else {
while (pq.size() != k) {
pq.pop();
}
cout << pq.top() << "\n";
}
}
return 0;
}
if you want to ask something about this feel free to ask
i hope this helps
if yes show your response with
and don’t forgot to mark doubt as resolved