zoukankan      html  css  js  c++  java
  • [LintCode] 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].

    Challenge 

    O(logn + k) time complexity.

    Algorithm

    1. Find the index of the number closest to target.

    2. Start from the number found in step 1, use two pointers to check both forward and backward, to get k closest numbers to the target.

     1 public class Solution {
     2     /**
     3      * @param A an integer array
     4      * @param target an integer
     5      * @param k a non-negative integer
     6      * @return an integer array
     7      */
     8     public int[] kClosestNumbers(int[] A, int target, int k) {
     9         if(k <= 0 || k > A.length) {
    10             return new int[0];
    11         }
    12         int idx = closetNumberIdxIteration(A, target, 0, A.length - 1);
    13         int left = idx - 1, right = idx + 1;
    14         int[] result = new int[k];
    15         result[0] = A[idx];
    16         for(int i = 1; i < k; i++) {
    17             if(left >= 0 && left < A.length && right >= 0 && right < A.length) {
    18                 if(Math.abs(A[left] - target) <= Math.abs(A[right] - target)) {
    19                     result[i] = A[left];
    20                     left--;
    21                 }
    22                 else {
    23                     result[i] = A[right];
    24                     right++;
    25                 }                
    26             }
    27             else if(left < 0 || left == A.length) {
    28                 result[i] = A[right];
    29                 right++;
    30             }
    31             else {
    32                 result[i] = A[left];
    33                 left--;
    34             }
    35         }
    36         return result;
    37     }
    38     private int closestNumberIdxRecursion(int[] A, int target, int lo, int hi)
    39     {
    40         if(hi - lo <= 1)
    41         {
    42             if(Math.abs(A[lo] - target) <= Math.abs(A[hi] - target))
    43             {
    44                 return lo;
    45             }
    46             else
    47             {
    48                 return hi;
    49             }
    50         }
    51         int mid = lo + (hi - lo) / 2;
    52         if(A[mid] == target)
    53         {
    54             return mid;
    55         }
    56         else if(A[mid] > target)
    57         {
    58             return closestNumberIdxRecursion(A, target, lo, mid);
    59         }
    60         else
    61         {
    62             return closestNumberIdxRecursion(A, target, mid, hi);
    63         }
    64     }
    65     private int closetNumberIdxIteration(int[] A, int target, int lo, int hi) {
    66         while(lo + 1 < hi) {
    67             int mid = lo + (hi - lo) / 2;
    68             if(A[mid] == target) {
    69                 return mid;
    70             }
    71             else if(A[mid] > target) {
    72                 hi = mid;
    73             }
    74             else {
    75                 lo = mid;
    76             }
    77         }
    78         return Math.abs(A[lo] - target) <= Math.abs(A[hi] - target) ? lo : hi;
    79     }
    80 }

    Related Problems

    K Closest Points

    Closest Number in Sorted Array

  • 相关阅读:
    async/await语法
    generator生成器函数
    数组练习
    解决异步(重点promise函数)
    iterator遍历器
    各种遍历方法(重点for....of)
    ES6代理proxy
    Symbol新数据类型
    函数(rest 箭头)
    ES6常用方法(字符串,数字,数组,对象)
  • 原文地址:https://www.cnblogs.com/lz87/p/7494252.html
Copyright © 2011-2022 走看看