Search for a Range

Question

Problem Statement

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

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

Example

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

Challenge

O(log n) time.

题解

Python

first/last position 结合。

  1. class Solution:
  2. """
  3. @param A : a list of integers
  4. @param target : an integer to be searched
  5. @return : a list of length 2, [index1, index2]
  6. """
  7. def searchRange(self, A, target):
  8. ret = [-1, -1]
  9. if not A:
  10. return ret
  11. # find the first position of target
  12. st, ed = 0, len(A) - 1
  13. while st + 1 < ed:
  14. mid = (st + ed) / 2
  15. if A[mid] == target:
  16. ed = mid
  17. elif A[mid] < target:
  18. st = mid
  19. else:
  20. ed = mid
  21. if A[st] == target:
  22. ret[0] = st
  23. elif A[ed] == target:
  24. ret[0] = ed
  25. # find the last position of target
  26. st, ed = 0, len(A) - 1
  27. while st + 1 < ed:
  28. mid = (st + ed) / 2
  29. if A[mid] == target:
  30. st = mid
  31. elif A[mid] < target:
  32. st = mid
  33. else:
  34. ed = mid
  35. if A[ed] == target:
  36. ret[1] = ed
  37. elif A[st] == target:
  38. ret[1] = st
  39. return ret

C++

  1. class Solution {
  2. public:
  3. vector<int> searchRange(vector<int>& nums, int target) {
  4. vector<int> result = {-1, -1};
  5. if (nums.empty()) return result;
  6. int lb = -1, ub = nums.size();
  7. while (lb + 1 < ub) {
  8. int mid = lb + (ub - lb) / 2;
  9. if (nums[mid] < target) lb = mid;
  10. else ub = mid;
  11. }
  12. if ((ub < nums.size()) && (nums[ub] == target)) result[0] = ub;
  13. else return result;
  14. ub = nums.size();
  15. while (lb + 1 < ub) {
  16. int mid = lb + (ub - lb) / 2;
  17. if (nums[mid] > target) ub = mid;
  18. else lb = mid;
  19. }
  20. result[1] = ub - 1;
  21. return result;
  22. }
  23. };

Java

lower/upper bound 的结合,做两次搜索即可。

  1. public class Solution {
  2. /**
  3. *@param A : an integer sorted array
  4. *@param target : an integer to be inserted
  5. *return : a list of length 2, [index1, index2]
  6. */
  7. public int[] searchRange(int[] A, int target) {
  8. int[] result = new int[]{-1, -1};
  9. if (A == null || A.length == 0) return result;
  10. int lb = -1, ub = A.length;
  11. // lower bound
  12. while (lb + 1 < ub) {
  13. int mid = lb + (ub - lb) / 2;
  14. if (A[mid] < target) {
  15. lb = mid;
  16. } else {
  17. ub = mid;
  18. }
  19. }
  20. // whether A[lb + 1] == target, check lb + 1 first
  21. if ((lb + 1 < A.length) && (A[lb + 1] == target)) {
  22. result[0] = lb + 1;
  23. } else {
  24. result[0] = -1;
  25. result[1] = -1;
  26. // target is not in the array
  27. return result;
  28. }
  29. // upper bound, since ub >= lb, we do not reset lb
  30. ub = A.length;
  31. while (lb + 1 < ub) {
  32. int mid = lb + (ub - lb) / 2;
  33. if (A[mid] > target) {
  34. ub = mid;
  35. } else {
  36. lb = mid;
  37. }
  38. }
  39. // target must exist in the array
  40. result[1] = ub - 1;
  41. return result;
  42. }
  43. }

源码分析

  1. 首先对输入做异常处理,数组为空或者长度为0
  2. 分 lower/upper bound 两次搜索,注意如果在 lower bound 阶段未找到目标值时,upper bound 也一定找不到。
  3. A[lb + 1] 时一定要注意判断索引是否越界!

复杂度分析

两次二分搜索,时间复杂度仍为 O(\log n).

Reference