[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

转载于:https://www.cnblogs.com/lz87/p/7494252.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值