-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap3.cpp
62 lines (56 loc) · 1.55 KB
/
heap3.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
// q4- K closest numbers to a given value
// which heap to us - how to do the sorting
// to solve we need to put in heap pair - key(absolute difference of given number) and the index of the key in the original
// arr to return the result
// so its max heap as we want the smallest value
// by default heap is max heap
// if we want min heap - (in case we are solving for something largest) then we put greater<int>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int *answer4(int *arr, int n, int k, int value)
{
priority_queue<pair<int, int>> max;
int *result = new int[k];
for (int i = 0; i < n; i++)
{
max.push(make_pair((abs(arr[i] - value)), i));
if (max.size() > k)
{
max.pop();
}
}
int j = 0;
while (!max.empty())
{
result[j] = arr[(max.top()).second];
j++;
max.pop();
}
return result;
}
// q4 again - K closest numbers to a given value
// min heap - could have used any heap
int *answer4_1(int *arr, int k, int value, int n)
{
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minHeap;
int *result = new int[k];
for (int i = 0; i < n; i++)
{
minHeap.push(make_pair(abs(arr[i] - value), i));
if (minHeap.size() > k)
{
minHeap.pop();
}
}
int j = 0;
while (!minHeap.empty())
{
result[j] = arr[minHeap.top().second];
minHeap.pop();
j++;
}
}
// Top K Frequent numbers
// this will be solved with the help of hash