# Time:  O(logn)
# Space: O(1)
#
# 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].
#

class Solution:
    # @param A, a list of integers
    # @param target, an integer to be searched
    # @return a list of length 2, [index1, index2]
    def searchRange(self, A, target):
        # Find the first index where target <= A[idx]
        left = self.binarySearch(lambda x, y: x <= y, A, target)
        if left >= len(A) or A[left] != target:
            return [-1, -1]
        # Find the first index where target < A[idx]
        right = self.binarySearch(lambda x, y: x < y, A, target)
        return [left, right - 1]
    
    def binarySearch(self, compare, A, target):
        start, end = 0, len(A)
        while start < end:
            mid = start + (end - start) / 2
            if compare(target, A[mid]):
                end = mid
            else:
                start = mid + 1
        return start
    
    def binarySearch2(self, compare, A, target):
        start, end = 0, len(A) - 1
        while start <= end:
            mid = start + (end - start) / 2
            if compare(target, A[mid]):
                end = mid - 1
            else:
                start = mid + 1
        return start
    
    def binarySearch3(self, compare, A, target):
        start, end = -1, len(A)
        while end - start > 1:
            mid = start + (end - start) / 2
            if compare(target, A[mid]):
                end = mid
            else:
                start = mid
        return end

if __name__ == "__main__":
    print Solution().searchRange([2, 2], 3)
    print Solution().searchRange([5, 7, 7, 8, 8, 10], 8)

@@ -367,180 +367,164 @@
 tion
-:%0A    # @param A, a list of integers%0A    # @param target, an integer to be searched%0A    # @return a list of length 2, %5Bindex1, index2%5D%0A    def searchRange(self, A, target):
+(object):%0A    def searchRange(self, nums, target):%0A        %22%22%22%0A        :type nums: List%5Bint%5D%0A        :type target: int%0A        :rtype: List%5Bint%5D%0A        %22%22%22
 %0A   
@@ -567,17 +567,20 @@
 rget %3C= 
-A
+nums
 %5Bidx%5D%0A  
@@ -627,25 +627,28 @@
  y: x %3C= y, 
-A
+nums
 , target)%0A  
@@ -672,15 +672,21 @@
 len(
-A
+nums
 ) or 
-A
+nums
 %5Blef
@@ -773,17 +773,20 @@
 arget %3C 
-A
+nums
 %5Bidx%5D%0A  
@@ -837,17 +837,20 @@
  x %3C y, 
-A
+nums
 , target
@@ -917,33 +917,36 @@
 (self, compare, 
-A
+nums
 , target):%0A     
@@ -944,34 +944,35 @@
 t):%0A        
-start, end
+left, right
  = 0, len(A)
@@ -965,25 +965,28 @@
 ht = 0, len(
-A
+nums
 )%0A        wh
@@ -993,19 +993,20 @@
 ile 
-start %3C end
+left %3C right
 :%0A  
@@ -1021,35 +1021,35 @@
   mid = 
-star
+lef
 t + (
-end - star
+right - lef
 t) / 2%0A 
@@ -1070,33 +1070,36 @@
 compare(target, 
-A
+nums
 %5Bmid%5D):%0A        
@@ -1098,35 +1098,37 @@
                 
-end
+right
  = mid%0A         
@@ -1144,36 +1144,35 @@
                 
-star
+lef
 t = mid + 1%0A    
@@ -1178,34 +1178,29 @@
      return 
-start%0A    
+left%0A
 %0A    def bin
@@ -1217,33 +1217,36 @@
 (self, compare, 
-A
+nums
 , target):%0A     
@@ -1248,26 +1248,27 @@
         
-start, end
+left, right
  = 0, le
@@ -1269,17 +1269,20 @@
  0, len(
-A
+nums
 ) - 1%0A  
@@ -1297,20 +1297,21 @@
 ile 
-star
+lef
 t %3C= 
-end
+right
 :%0A  
@@ -1326,35 +1326,35 @@
   mid = 
-star
+lef
 t + (
-end - star
+right - lef
 t) / 2%0A 
@@ -1375,33 +1375,36 @@
 compare(target, 
-A
+nums
 %5Bmid%5D):%0A        
@@ -1407,27 +1407,29 @@
             
-end
+right
  = mid - 1%0A 
@@ -1457,28 +1457,27 @@
             
-star
+lef
 t = mid + 1%0A
@@ -1495,18 +1495,13 @@
 urn 
-start%0A    
+left%0A
 %0A   
@@ -1534,17 +1534,20 @@
 ompare, 
-A
+nums
 , target
@@ -1561,18 +1561,19 @@
     
-start, end
+left, right
  = -
@@ -1579,17 +1579,20 @@
 -1, len(
-A
+nums
 )%0A      
@@ -1599,26 +1599,27 @@
   while 
-end - star
+right - lef
 t %3E 1:%0A 
@@ -1639,27 +1639,27 @@
 d = 
-star
+lef
 t + (
-end - star
+right - lef
 t) /
@@ -1696,9 +1696,12 @@
 et, 
-A
+nums
 %5Bmid
@@ -1720,19 +1720,21 @@
         
-end
+right
  = mid%0A 
@@ -1766,20 +1766,19 @@
         
-star
+lef
 t = mid%0A
@@ -1792,19 +1792,22 @@
  return 
-end
+right%0A
 %0A%0Aif __n

Update search-for-a-range.py