[LintCode] Search for a Range

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.

Break this problem into two subproblems:

1. find the first element in the array of the given target value;

2. find the last element in the array of the given target value;

Both of these two subproblems can be solved by twisting the regular binary search algorithm when the target value is found.

To find the first occurence,  keep looking its left range, including the mid element;

To find the last occurence,  keep looking its right range, including the mid element;

 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[2];
 9         result[0] = -1;
10         result[1] = -1;
11         if(A == null || A.length == 0)
12         {
13             return result;
14         }
15         result[0] = searchRangeLeft(A, target, 0, A.length - 1);
16         if(result[0] < 0)
17         {
18             return result;
19         }
20         else
21         {
22             result[1] = searchRangeRight(A, target, 0, A.length - 1);
23         }
24         return result;
25     }
26     private int searchRangeLeft(int[] A, int target, int lo, int hi)
27     {
28         if(hi - lo <= 1)
29         {
30             if(A[lo] == target)
31             {
32                 return lo;
33             }
34             else if(A[hi] == target)
35             {
36                 return hi;
37             }
38             else
39             {
40                 return -1;
41             }
42         }
43         int mid = lo + (hi - lo) / 2;
44         if(A[mid] == target)
45         {
46             return searchRangeLeft(A, target, lo, mid);
47         }
48         else if(A[mid] > target)
49         {
50             return searchRangeLeft(A, target, lo, mid - 1);
51         }
52         else
53         {
54             return searchRangeLeft(A, target, mid + 1, hi);
55         }
56     }
57     private int searchRangeRight(int[] A, int target, int lo, int hi)
58     {
59         if(hi - lo <= 1)
60         {
61             if(A[hi] == target)
62             {
63                 return hi;
64             }
65             else if(A[lo] == target)
66             {
67                 return lo;
68             }
69             else
70             {
71                 return -1;
72             }
73         }
74         int mid = lo + (hi - lo) / 2;
75         if(A[mid] == target)
76         {
77             return searchRangeRight(A, target, mid, hi);
78         }
79         else if(A[mid] > target)
80         {
81             return searchRangeRight(A, target, lo, mid - 1);
82         }
83         else
84         {
85             return searchRangeRight(A, target, mid + 1, hi);
86         }
87     }
88 }

Related Problems

Total Occurrence of Target

Range Sum Query 2D - Immutable

时间: 2024-10-20 07:41:29

[LintCode] Search for a Range的相关文章

LintCode Search For a Range (Binary Search)

Binary Search模板: mid 和 target 指针比较,left/ right 和 target 比较. 循环终止条件: 最后剩两数比较(while(left + 1 < right)). 循环结束后根据要求检查最后两个数(left/ right 和 target 比较). public class Solution { /** *@param A : an integer sorted array *@param target : an integer to be inserte

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Given a sorted array of n integers, find the starting and ending position of a given target va

[Leetcode + Lintcode] 34. 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,

Leetcode 34. Search for a Range

34. Search for a Range Total Accepted: 91570 Total Submissions: 308037 Difficulty: Medium 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(l

【LeetCode】Search for a Range

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 ex

leetcode 二分查找 Search for a Range

Search for a Range Total Accepted: 21480 Total Submissions: 78454My Submissions 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

[LeetCode] 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,

[LeetCode] 034. Search for a Range (Medium) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search for a Range (Medium) 链接: 题目:https://leetcode.com/problems/search-for-a-range/ 代码(github):https://github.com/illuz/leetcode 题意: 在有序数组中找到一个数的范围.(由于数

[leetcode]Search for a Range @ Python

原题地址:https://oj.leetcode.com/problems/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