Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

解法1

Analysis

Helper function:
用binary search找到第一次出现的index low =0, high = length (low inclusive, high exclusive)

  1. While condition: low<high
  2. mid = (low+high)/2
  3. If mid < target, left boundary is on the right, low=mid+1
  4. If mid > target, left boundary is on the left, high = mid
  5. If mid = target, left boundary is either on the left or mid, high = mid+1 (but if use mid+1, will cause dead cycle, try example with high = mid and works, merge with 3)
    综上所述,和标准的binary search唯一不同的是mid=target的情况和mid>target合并
    跳出while时,如果array里有target,则low指向以一个target,如果没有,则low指向target应该插入的地方
    返回low

Main function:

  1. 找出target第一次出现的位置为left,没有出现这个数的情况有两种:
    1. left==nums.length (target的值大于所有nums里的数)
    2. nums[left]!=target(target的值在nums范围里或者小于所有数)
      注意的一定要先判断第一个条件,否则第二个条件会out of bound
  2. 如果不是上面两种情况的话则一定找到了target,这时左边界就是left,右边界是 binarySearch(nums, target + 1) - 1

Code

public class Solution {
    public int[] searchRange(int[] nums, int target) {
        int left = binarySearch(nums, target);
        if (left == nums.length || nums[left] != target) {
            return new int[]{-1, -1};
        }
        return new int[]{left, binarySearch(nums, target + 1) - 1};
    }

    private int binarySearch(int[]nums, int target){
        int low=0, high = nums.length;
        while(low<high){
            int mid = low+(high-low)/2;
            if (nums[mid]<target){low=mid+1;}
            if (nums[mid]>=target){high=mid;}
        }
        return low;
    }
}

Note

还有一种解法是找一次左边界,一次右边界,会比以上方法复杂,因为要写两种不同条件的binary search。具体参照
Ref1 Ref2

Reference

Ref1

解法2:

碰到mid==target的情况把左右边界++,--。performance没那么好,代码比较容易记住。

public class Solution {
    public int[] searchRange(int[] nums, int target) {
        int[] res = new int[] {-1, -1};
        if (nums == null || nums.length ==0) return res;

        int start=0, end = nums.length-1;

        while (start + 1 < end){
            int mid = start + (end - start)/2;
            if (nums[mid] == target){
                if (nums[start]!=target) start++;
                if (nums[end]!=target) end--;
                if (nums[start]==target && nums[end]==target) break;
            }
            else if (nums[mid] < target){
                start = mid;
            }
            else {
                end = mid;
            }
        }

        if (nums[start]==target && nums[end]==target) {res[0] = start; res[1] = end;}
        else if (nums[start]==target) {res[0] = start; res[1] = start;}
        else if (nums[end]==target) {res[0] = end; res[1] = end;}

        return res;
    }
}

解法3:

左右边界找两次

public class Solution {
    public int[] searchRange(int[] A, int target) {
        if (A.length == 0) {
            return new int[]{-1, -1};
        }

        int start, end, mid;
        int[] bound = new int[2]; 

        // search for left bound
        start = 0; 
        end = A.length - 1;
        while (start + 1 < end) {
            mid = start + (end - start) / 2;
            if (A[mid] == target) {
                end = mid;
            } else if (A[mid] < target) {
                start = mid;
            } else {
                end = mid;
            }
        }
        if (A[start] == target) {
            bound[0] = start;
        } else if (A[end] == target) {
            bound[0] = end;
        } else {
            bound[0] = bound[1] = -1;
            return bound;
        }

        // search for right bound
        start = 0;
        end = A.length - 1;
        while (start + 1 < end) {
            mid = start + (end - start) / 2;
            if (A[mid] == target) {
                start = mid;
            } else if (A[mid] < target) {
                start = mid;
            } else {
                end = mid;
            }
        }
        if (A[end] == target) {
            bound[1] = end;
        } else if (A[start] == target) {
            bound[1] = start;
        } else {
            bound[0] = bound[1] = -1;
            return bound;
        }

        return bound;
    }
}

Reference: jiuzhang