K Closest Numbers In Sorted Array

Given a target number, 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

Given A = [1, 2, 3], target = 2 and k = 3, return [2, 1, 3].

Given A = [1, 4, 6, 8], target = 3 and k = 3, return [4, 1, 6].

Solution: 

     /* findFirst() is to find the first index of number that is larger or equal to target, the way is binary search

  * "target - A[start] >= A[end] - target" is keep order ascending when left and right have same diff

   */

  public int[] kClosestNumbers(int[] A, int target, int k) {

    int[] ret = new int[k];

    if (A == null || A.length < k) {

      return ret;

    }

    int start = findFirst(A, target) - 1;

    int end = start + 1;

    for (int i = 0; i < k; i++) {

      if (start < 0) {

        ret[i] = A[end++];

      } else if (end >= A.length) {

        ret[i] = A[start--];

      } else {

        if (target - A[start] >= A[end] - target) {

          ret[i] = A[start--];

        } else {

          ret[i] = A[end++];

        }

      }

    }

    return ret;

  }

private int findFirst(int[] A, int target) {

  if (A == null || A.length == 0) {

    return -1;    

  }

  int start = 0;

  int end = A.length - 1;

  int mid = 0;

  while (start + 1 < end) {

    mid = start + (end - start) / 2;

    if (A[mid] < target) {

      start = mid;

    } else {

      end = mid;

    }

  }

  if (A[start] >= target) {

    return start;

  } else if (A[end] >= target) {

    return end;

  } else {

    return A.length;

  }

}

时间: 2024-10-10 00:43:05

K Closest Numbers In Sorted Array的相关文章

[LintCode] 459 Closest Number in Sorted Array

DescriptionGiven a target number and an integer array A sorted in ascending order, find the index i in A such that A[i] is closest to the given target. Return -1 if there is no element in the array. NoticeThere can be duplicate elements in the array,

Closest Number in Sorted Array

public class Solution { /** * @param A an integer array sorted in ascending order * @param target an integer * @return an integer */ public int closestNumber(int[] A, int target) { // Write your code here if (A == null || A.length == 0) { return -1;

K Closest In Sorted Array - Medium

Given a target integer T, a non-negative integer K and an integer array A sorted in ascending order, find the K closest numbers to T in A. Assumptions A is not null K is guranteed to be >= 0 and K is guranteed to be <= A.length Return A size K integ

Closet Number in sorted array

Closest Number in Sorted Array Given a target number and an integer array A sorted in ascending order, find the index i in A such that A[i] is closest to the given target. Return -1 if there is no element in the array. Example Given [1, 2, 3] and tar

1296. Divide Array in Sets of K Consecutive Numbers

Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbersReturn True if its possible otherwise return False. Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output:

658. Find K Closest Elements

Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred. Example 1: Input: [1,2,3,4,5], k=4, x=3 Outp

leetcode 658. Find K Closest Elements

Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred. Example 1: Input: [1,2,3,4,5], k=4, x=3 Outp

LeetCode Find K Closest Elements

原题链接在这里:https://leetcode.com/problems/find-k-closest-elements/description/ 题目: Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smal

LeetCode 1060. Missing Element in Sorted Array

原题链接在这里:https://leetcode.com/problems/missing-element-in-sorted-array/ 题目: Given a sorted array A of unique numbers, find the K-th missing number starting from the leftmost number of the array. Example 1: Input: A = [4,7,9,10], K = 1 Output: 5 Explan