-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap8.cpp
111 lines (86 loc) · 2.81 KB
/
heap8.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
// K -closed points to origin
// Given a list of points on a 2D plane, the task is to find the K closest points to the origin (0, 0). The distance between two points (x1, y1) and (x2, y2) is given by the Euclidean distance formula:
// distance = sqrt(x1^2-x2^2 + y1^2-y2^2)
// for this question it'll be sqrt(x1^2+y1^2) as we are talking about origin
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// vector<pair<int, int>> answer(vector<pair<int, int>> arr, int k)
// {
// vector<pair<int, int>> result;
// // min heap
// priority_queue<pair<float, pair<int, int>>, vector<pair<int, int>>, greater<pair<float, pair<int, int>>>> min;
// int n = arr.size();
// int distance;
// for (int i = 0; i < n; i++)
// {
// distance = math.sqrt(arr[i].first * arr[i].first + arr[i].second * arr[i].second);
// min.push({distance, {arr[i].first, arr[i].second}});
// if (min.size() > K)
// {
// min.pop();
// }
// }
// while (!min.empty())
// {
// result.push_back(min.top().second);
// min.pop();
// }
// return result;
// }
// vector<pair<int, int>> answer(vector<pair<int, int>> arr, int k)
// {
// vector<pair<int, int>> result;
// // we need max heap
// // priority_queue<pair<float, pair<int, int>>, vector<pair<int, int>>, greater<pair<float, pair<int, int>>>> min;
// int n = arr.size();
// // didn't change variable type after changing it in queue
// int distance;
// for (int i = 0; i < n; i++)
// {
// // sqrt() not math.
// distance = math.sqrt(arr[i].first * arr[i].first + arr[i].second * arr[i].second);
// min.push({distance, {arr[i].first, arr[i].second}});
// if (min.size() > K)
// {
// min.pop();
// }
// }
// // rest logic is good
// while (!min.empty())
// {
// result.push_back(min.top().second);
// min.pop();
// }
// return result;
// }
// gpt reviewed answer
#include <cmath> // for sqrt
using namespace std;
vector<pair<int, int>> answer(vector<pair<int, int>> arr, int k)
{
vector<pair<int, int>> result;
// min heap with pair<distance, point>
priority_queue<pair<float, pair<int, int>>> min;
int n = arr.size();
float distance;
for (int i = 0; i < n; i++)
{
distance = sqrt(arr[i].first * arr[i].first + arr[i].second * arr[i].second);
// Add point to min heap
min.push({distance, {arr[i].first, arr[i].second}});
// Keep only the top K closest points
if (min.size() > k)
{
min.pop();
}
}
// Extract the k closest points from the heap
while (!min.empty())
{
result.push_back(min.top().second);
min.pop();
}
return result;
}