Given
target
, a non-negative integer k
and an integer array A
sorted in ascending order, find the k
closest numbers to target
in A
, sorted in ascending order by the difference between the number and target. Otherwise, sorted in ascending order by number if the difference is same.Example
Example 1:
Input: A = [1, 2, 3], target = 2, k = 3
Output: [2, 1, 3]
Example 2:
Input: A = [1, 4, 6, 8], target = 3, k = 3
Output: [4, 1, 6]
Challenge
O(logn + k) time
Notice
- The value
k
is a non-negative integer and will always be smaller than the length of the sorted array. - Length of the given array is positive and will not exceed
- Absolute value of elements in the array will not exceed
Code (Java):
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 | public class Solution { /** * @param A: an integer array * @param target: An integer * @param k: An integer * @return: an integer array */ public int [] kClosestNumbers( int [] A, int target, int k) { // write your code here int [] ans = new int [k]; if (A == null || A.length == 0 || k == 0 ) { return ans; } // step 1: find the first number >= target // int right = binarySearchGreater(A, target); int left = right - 1 ; for ( int i = 0 ; i < k; i++) { if (left < 0 ) { ans[i] = A[right++]; } else if (right >= A.length) { ans[i] = A[left--]; } else if (Math.abs(A[left] - target) <= Math.abs(A[right] - target)) { ans[i] = A[left--]; } else { ans[i] = A[right++]; } } return ans; } private int binarySearchGreater( int [] A, int target) { int start = 0 ; int end = A.length - 1 ; while (start + 1 < end) { int mid = start + (end - start) / 2 ; if (A[mid] == target) { return mid; } else if (A[mid] > target) { end = mid; } else { start = mid + 1 ; } } if (A[start] >= target) { return start; } if (A[end] >= target) { return end; } return A.length - 1 ; } } |
No comments:
Post a Comment