-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchfora2dmatrix.py
200 lines (175 loc) · 6.78 KB
/
Searchfora2dmatrix.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#You are given an m x n integer matrix matrix with the following two properties:
#Each row is sorted in non-decreasing order.
#The first integer of each row is greater than the last integer of the previous row.
#Given an integer target, return true if target is in matrix or false otherwise.
#You must write a solution in O(log(m * n)) time complexity.
#mysolution:
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if target == matrix[i][j]:
return True
return False
#5/20/24 (missed) but my own solution in python3:
#the core idea behind this solution is we have to run binary search not only on each sublist in the input, but we also have to run binary search on each row and see if each element in the row matches target
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
l, r = 0, len(matrix) - 1
while l <= r:
mid = (l + r) // 2
if matrix[mid][0] <= target <= matrix[mid][-1]: #we need to perform binary search in each row of the list of lists
row = matrix[mid]
lr, rr = 0, len(row) - 1
while lr <= rr:
midrow = (lr + rr) // 2
if row[midrow] < target:
lr = midrow + 1 #always move l or r pointers, not mid! we are using l and r to calculate mid to close the search space!
elif row[midrow] > target:
rr = midrow - 1
else:
return True
return False #the idea is that, since rows are sorted in ascending order, if the target exists in the entire matrix, it would be in this row, but it's not, so we can return False
elif matrix[mid][0] > target:
r = mid - 1
else:
l = mid + 1
return False
#practice again:
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
l, r = 0, len(matrix) - 1
while l <= r:
mid = (l + r) // 2
if matrix[mid][0] <= target <= matrix[mid][-1]:
row = matrix[mid]
lr, rr = 0, len(row) - 1
while lr <= rr:
midr = (lr + rr) // 2
if row[midr] < target:
lr = midr + 1
elif row[midr] > target:
rr = midr - 1
else:
return True
return False
elif matrix[mid][0] > target:
r = mid - 1
else: #matrix[mid][-1] < target: l = mid + 1 also works here too instead of else!
l = mid + 1
return False
#5/21/24 refresher:
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
l, r = 0, len(matrix) - 1
while l <= r:
mid = (l + r) // 2
if matrix[mid][0] <= target <= matrix[mid][-1]:
row = matrix[mid] #[10, 11, 16, 20]
lr, rr = 0, len(row) - 1
while lr <= rr:
midr = (lr + rr) // 2
if row[midr] == target:
return True
elif row[midr] < target:
lr = midr + 1
else:
rr = midr - 1
return False
elif matrix[mid][0] > target:
r = mid - 1
else:
l = mid + 1
return False
#5/22/24:
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
l, r = 0, len(matrix) - 1
while l <= r:
mid = (l + r) // 2
if matrix[mid][0] <= target <= matrix[mid][-1]:
row = matrix[mid]
lr, rr = 0, len(row) - 1
while lr <= rr:
midr = (lr + rr) // 2
if row[midr] == target:
return True
elif row[midr] > target:
rr = midr - 1
else:
lr = midr + 1
return False #supposed to be in this row but wasn't
if matrix[mid][0] > target:
r = mid - 1
else:
l = mid + 1
return False
#5/25/24 practice:
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
l, r = 0, len(matrix) - 1
while l <= r:
mid = (l + r) // 2
if matrix[mid][0] <= target <= matrix[mid][-1]:
row = matrix[mid]
lr, rr = 0, len(row) - 1
while lr <= rr:
midr = (lr + rr) // 2
if row[midr] == target:
return True
elif row[midr] > target:
rr = midr - 1
else:
lr = midr + 1
return False
elif matrix[mid][0] > target:
r = mid - 1
else:
l = mid + 1
return False
#6/14/24 review:
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
l, r = 0, len(matrix) - 1
while l <= r:
mid = (l + r) // 2
row = matrix[mid]
if row[0] <= target <= row[-1]:
lr, rr = 0, len(row) - 1
while lr <= rr:
midr = (lr + rr) // 2
if row[midr] == target:
return True
elif row[midr] < target:
lr = midr + 1
else:
rr = midr - 1
return False
elif target > row[-1]:
l = mid + 1
else:
r = mid - 1
return False
#7/26/24 refresher:
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
l, r = 0, len(matrix) - 1
while l <= r:
mid = (l + r) // 2
currentrow = matrix[mid]
if target in currentrow:
lr, rr = 0, len(currentrow) - 1
while lr <= rr:
mid = (lr + rr) // 2
if currentrow[mid] == target:
return True
elif currentrow[mid] > target:
rr = mid - 1
else:
lr = mid + 1
return False
elif target < currentrow[0]:
r = mid - 1
else:
l = mid + 1
return False