diff options
Diffstat (limited to 'find_k_closest_elements.cpp')
-rw-r--r-- | find_k_closest_elements.cpp | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/find_k_closest_elements.cpp b/find_k_closest_elements.cpp index ecc1f53..e15810c 100644 --- a/find_k_closest_elements.cpp +++ b/find_k_closest_elements.cpp @@ -1,16 +1,37 @@ +// 658. Find K Closest Elements #include "leetcode.h" +/* + * 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. an integer 'a' is closer to 'x' than an integer 'b' if '|a - + * x| < |b - x|' or '|a - x| == |b - x|' and 'a < b'. + */ + class Solution { public: vector<int> findClosestElements(vector<int> &arr, int k, int x) { - int left = 0, right = arr.size() - k; - while (left < right) { - int mid = (left + right) / 2; - if (x - arr[mid] > arr[mid + k] - x) - left = mid + 1; + int l = 0, r = arr.size() - k; + while (l < r) { + int m = (l + r) / 2; + if (x - arr[m] > arr[m + k] - x) + l = m + 1; else - right = mid; + r = m; } - return vector<int>(arr.begin() + left, arr.begin() + left + k); + return vector<int>(arr.begin() + l, arr.begin() + l + k); } }; + +int main() { + Solution obj; + vector<int> a1 = {1, 2, 3, 4, 5}, a2 = {1, 2, 3, 4, 5}; + vector<int> fce1 = obj.findClosestElements(a1, 4, 3); + vector<int> fce2 = obj.findClosestElements(a2, 4, -1); + for (int i = 0; i < fce1.size(); i++) + printf("%d ", fce1[i]); // expect: 1 2 3 4 + printf("\n"); + for (int i = 0; i < fce2.size(); i++) + printf("%d ", fce2[i]); // expect: 1 2 3 4 + printf("\n"); +} |