思考: 要維護一個前 k 小的點要用 max-heap 要維護一個前 k 大的點要用 min-heap 這題目的答案是要回傳 點 heap裡面要記錄的是 距離, x, y ```c++= class Solution { public: vector<vector<int>> kClosest(vector<vector<int>>& points, int k) { // 用 max-heap 可以在新的距離來的時候比較頂點 如果 比較小就 pop掉頂點放入新的值 // 找 前 k 小的值用 max-heap 找 前 k 大用 min-heap priority_queue <vector<int>> maxheap; vector<vector<int>> ans(k); int n = points.size(); for(int i = 0; i < n; i++){ int dis = pow(points[i][0], 2) + pow(points[i][1], 2); maxheap.push({dis, points[i][0], points[i][1]}); if(maxheap.size() > k){ maxheap.pop(); } } for(int i = 0; i < k; i++){ auto top = maxheap.top(); maxheap.pop(); ans[i] = {top[1], top[2]}; } return ans; } }; ```