|
| 1 | +/* |
| 2 | + Author: SUYASH SINGH |
| 3 | +
|
| 4 | +Problem statement:-Find K Closest Elements |
| 5 | +Given a sorted integer array arr, two integers k and x, return the k closest integers to x in the array. The result should also be sorted in ascending order. |
| 6 | +
|
| 7 | +An integer a is closer to x than an integer b if: |
| 8 | +
|
| 9 | +|a - x| < |b - x|, or |
| 10 | +|a - x| == |b - x| and a < b |
| 11 | +Explaination: |
| 12 | +
|
| 13 | +Input: points = [[1,3],[-2,2]], k = 1 |
| 14 | +Output: [[-2,2]] |
| 15 | +Explanation: |
| 16 | +The distance between (1, 3) and the origin is sqrt(10). |
| 17 | +The distance between (-2, 2) and the origin is sqrt(8). |
| 18 | +Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. |
| 19 | +We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]]. |
| 20 | +Example 2: |
| 21 | +Input: points = [[3,3],[5,-1],[-2,4]], k = 2 |
| 22 | +Output: [[3,3],[-2,4]] |
| 23 | +Explanation: The answer [[-2,4],[3,3]] would also be accepted. |
| 24 | +*/ |
| 25 | + |
| 26 | +#include <iostream> |
| 27 | +#include <vector> |
| 28 | +#include <queue> |
| 29 | +#include <cmath> |
| 30 | + |
| 31 | +using namespace std; |
| 32 | + |
| 33 | +class Solution { |
| 34 | +public: |
| 35 | + vector<vector<int>> kClosest(vector<vector<int>>& points, int k) { |
| 36 | + // Answer vector |
| 37 | + vector<vector<int>> result(k); |
| 38 | + // Max heap storage initialization |
| 39 | + priority_queue<vector<int>> maxHeap; |
| 40 | + // Construction of max heap |
| 41 | + for (auto& p : points) { |
| 42 | + int x = p[0], y = p[1]; |
| 43 | + // Calculate the squared distance from the origin using the formula x^2 + y^2 |
| 44 | + // Store the distance along with the coordinates (x, y) in the maxHeap |
| 45 | + maxHeap.push({x * x + y * y, x, y}); |
| 46 | + // If the size of the maxHeap exceeds k, remove the point with the maximum distance |
| 47 | + if (maxHeap.size() > k) { |
| 48 | + maxHeap.pop(); |
| 49 | + } |
| 50 | + } |
| 51 | + // Extract the k closest points from the maxHeap and store them in the result vector |
| 52 | + for (int i = k - 1; i >= 0; --i) { |
| 53 | + vector<int> top = maxHeap.top(); |
| 54 | + maxHeap.pop(); |
| 55 | + result[i] = {top[1], top[2]}; |
| 56 | + } |
| 57 | + |
| 58 | + // Return the result vector containing the k closest points |
| 59 | + |
| 60 | + return result; |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +int main() { |
| 65 | + vector<vector<int>> points = {{1, 3}, {-2, 2}, {5, -1}, {0, 0}, {3, 4}}; |
| 66 | + int k = 3; |
| 67 | + |
| 68 | + Solution solution; |
| 69 | + vector<vector<int>> closestPoints = solution.kClosest(points, k); |
| 70 | + |
| 71 | + cout << "The " << k << " closest points to the origin are:\n"; |
| 72 | + for (const auto& point : closestPoints) { |
| 73 | + cout << "(" << point[0] << ", " << point[1] << ")\n"; |
| 74 | + } |
| 75 | + |
| 76 | + return 0; |
| 77 | +}/* |
| 78 | +Time Complexity: |
| 79 | +
|
| 80 | +Constructing the maxHeap: the overall time complexity is O(N log K). |
| 81 | +Extracting the k closest points: O(K log K). We extract the top element from the maxHeap k times, each operation taking O(log K) time. Hence, the time complexity is O(K log K). |
| 82 | +Therefore, the overall time complexity of the solution is O(N log K), where N is the number of points and K is the value of k. |
| 83 | +
|
| 84 | +Space Complexity: |
| 85 | +
|
| 86 | +The maxHeap stores at most k elements, so the space complexity for the maxHeap is O(K). |
| 87 | +The result vector stores k closest points, resulting in O(K) space. |
| 88 | +Apart from these, the solution uses a constant amount of space for variables and temporary storage.*/ |
0 commit comments