-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathsearch.py
92 lines (82 loc) · 2.73 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
You are given an array of integers sorted in non-decreasing order, find the starting
and ending position of a given target value. If target is not found in the array,
return [-1, -1]. You must write an algorithm with O(logn) runtime complexity
"""
def start_end_sorted_array(nums, target):
left_extreme = left_index(nums, target)
right_extreme = right_index(nums, target)
return [left_extreme, right_extreme]
def left_index(nums,target):
left = 0
right = len(nums)-1
while left <= right:
middle = (left + right) // 2
if target == nums[middle]:
if middle == 0:
return middle
if nums[middle -1] == target:
right = middle-1
else:
return middle
else:
if target < nums[middle]:
right = middle -1
else:
left = middle + 1
return -1
def right_index(nums,targe):
left = 0
right = len(nums)-1
while left <= right:
middle = (left + right) // 2
if nums[middle] == target:
if middle == len(nums)-1:
return middle
if nums[middle + 1] == target:
left = middle+1
else:
return middle
else:
if target > nums[middle]:
left = middle + 1
else:
right = middle - 1
return -1
nums = [1,2,3,3,3,3,4,5]
target = 3
print(start_end_sorted_array(nums, target))
"""
Search in Matrix: Write an efficient algorithm that searches for a value target in
an m x n integer matrix. This matrix has the following properties:
- Integers in each row are sorted from left to right
- The first integer of each row is greater than the last integer of the previous row
If the value is there in the matrix, retrun true, else false.
"""
def search_matrix(matrix, target):
top = 0
bottom = len(matrix)-1
# Find the array that contains the target and store it in nums
while top <= bottom:
middle = (top + bottom) // 2
if matrix[middle][0] <= target and matrix[middle][-1] >= target:
nums = matrix[middle]
if matrix[middle][0] > target:
bottom = middle - 1
else:
top = middle + 1
# Use binary search to find the target in the array above
left = 0
right = len(nums)-1
while left <= right:
midpoint = (left + right) // 2
if nums[midpoint] == target:
return True
if nums[midpoint] > target:
right = midpoint - 1
else:
left = midpoint + 1
return False
matrix = [[1,5,7,11], [12,13,17,20], [25,26,30,31], [32,35,39,43], [45,60,62,65]]
target = 62
print(search_matrix(matrix, target))