Skip to content

Commit

Permalink
leetcode-658
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinayak409 committed Oct 4, 2022
1 parent 6700740 commit 1d3bd44
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Leetcode Solution/Heap/Medium/658. Find K Closest Elements.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
vector<int> v;
priority_queue<pair<int, int>> maxh;
for(int val : arr){
maxh.push({abs(val-x), val});
if(maxh.size() > k) maxh.pop();
}
while(maxh.size() > 0){
v.push_back(maxh.top().second);
maxh.pop();
}
sort(v.begin(), v.end());
return v;
}
};

0 comments on commit 1d3bd44

Please sign in to comment.