• [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

  • 相关阅读:
    ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
    MVC Razor模板引擎 @RenderBody、@RenderPage、@RenderSection及Html.RenderPartial、Html.RenderAction
    二叉树的建立&&前中后遍历(递归实现)&&层次遍历
    实现一个简单的散列表(HashMap)
    单向链表的删除及插入操作(以头插入法建立单向链表)
    单向链表的建立(头插入法)
    单向链表的建立(尾部插入法)
    链式队列(单向列表实现)
    顺序队列(数组实现)
    链式栈(单向链表实现)
  • 原文地址:https://www.cnblogs.com/lz87/p/7494252.html
Copyright © 2020-2023  润新知