-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsearch-a-2d-matrix-ii.py
245 lines (220 loc) · 9.72 KB
/
search-a-2d-matrix-ii.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# 240. Search a 2D Matrix II
# 🟠 Medium
#
# https://leetcode.com/problems/search-a-2d-matrix-ii/
#
# Tags: Array - Binary Search - Divide and Conquer - Matrix
import timeit
from typing import List, Tuple
# 1e3 calls:
# » BinarySearch2D 0.01545 seconds
# » LinearSearch 0.00551 seconds
# » MixedSearch 0.01432 seconds
# We can use binary search updating two pointers, row and col.
#
# Time complexity; O(log(n)) - Each iteration we discard three quarters of the input.
# Space complexity; O(1) - If we don't consider the input matrix.
#
# Runtime: 199 ms, faster than 78.11% of Python3 online submissions for Search a 2D Matrix II.
# Memory Usage: 20.6 MB, less than 8.29% of Python3 online submissions for Search a 2D Matrix II.
class BinarySearch2D:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
# Recursive call with the section of the matrix that could contain the target.
def searchRange(top_left: Tuple[int], bottom_right: Tuple[int]) -> bool:
# Once we have a small enough matrix 3x3, check all the values.
if top_left[0] - bottom_right[0] < 3 and top_left[1] - bottom_right[1] < 3:
for row_idx in range(top_left[0], bottom_right[0] + 1):
for col_idx in range(top_left[1], bottom_right[1] + 1):
if matrix[row_idx][col_idx] == target:
return True
return False
# If we have a bigger than 3x3 matrix, divide it into smaller sections.
# Get the middle value and use it to determine which side of the matrix we need to explore.
mid_row = top_left[0] + ((bottom_right[0] - top_left[0]) // 2)
mid_col = top_left[1] + ((bottom_right[1] - top_left[1]) // 2)
val = matrix[mid_row][mid_col]
if target < val:
# We want to search from the current top-left to the current (mid_row, mid_col).
return searchRange(top_left, (mid_row, mid_col))
elif target == val:
return True
else:
# We want to search two ranges, to the right and below the current row taking care not to go out of bounds.
if mid_row < len(matrix) - 1 and mid_col < len(matrix[0]) - 1:
return searchRange((top_left[0], mid_col), (mid_row, bottom_right[1])) or searchRange(
(mid_row + 1, top_left[1]), bottom_right
)
elif mid_row < len(matrix) - 1:
return searchRange((top_left[0], mid_col), (mid_row, bottom_right[1]))
elif mid_col < len(matrix[0]) - 1:
return searchRange((mid_row + 1, top_left[1]), bottom_right)
return False
# Initial call, search the entire matrix.
return searchRange((0, 0), (len(matrix) - 1, len(matrix[0]) - 1))
# We can improve on the O(log(n)) solution and search in linear n+m time starting at the bottom and moving:
#
# - One column to the right, if the value is smaller than the target.
# - One row up, if the value is greater than the target.
#
# Time complexity: O(n+m) - n: number of rows, m: number of columns, we visit each row and column a maximum of 1 time.
# Space complexity: O(1) - We only store two pointers in memory.
#
# Runtime: 176 ms, faster than 94.26% of Python3 online submissions for Search a 2D Matrix II.
# Memory Usage: 20.4 MB, less than 84.45% of Python3 online submissions for Search a 2D Matrix II.
class LinearSearch:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
row, col = len(matrix) - 1, 0
while row >= 0 and col < len(matrix[0]):
val = matrix[row][col]
if val == target:
return True
# If it does not match, move in function of the current value
if val < target:
# If the current value is less than the target, we want to check the values to the right.
col += 1
else:
# If the current value is greater than the target, we want to check values above.
row -= 1
# If the search went out of bounds, the value is not in the matrix.
return False
# We can also mix the two previous solutions. Use binary search when the size of the input matrix is big, then move
# to linear search once we have a smaller matrix.
#
# Intuitively this should perform better than either of the two previous solutions but it isn't the case.
#
# Runtime: 317 ms, faster than 24.54% of Python3 online submissions for Search a 2D Matrix II.
# Memory Usage: 20.7 MB, less than 8.29% of Python3 online submissions for Search a 2D Matrix II.
class MixedSearch:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
# Recursive call with the section of the matrix that could contain the target.
def searchRange(top_left: Tuple[int], bottom_right: Tuple[int]) -> bool:
limit = 20
# Once we have a small enough matrix limit x limit, use linear search.
if top_left[0] - bottom_right[0] < limit and top_left[1] - bottom_right[1] < limit:
# Start at the bottom-left corner
row, col = len(matrix) - 1, 0
while row >= top_left[0] and col < bottom_right[1] + 1:
val = matrix[row][col]
if val == target:
return True
# If it does not match, move in function of the current value
if val < target:
# If the current value is less than the target, we want to check the values to the right.
col += 1
else:
# If the current value is greater than the target, we want to check values above.
row -= 1
# If the search went out of bounds, the value is not in the matrix.
return False
# If we have a bigger than 3x3 matrix, divide it into smaller sections.
# Get the middle value and use it to determine which side of the matrix we need to explore.
mid_row = top_left[0] + ((bottom_right[0] - top_left[0]) // 2)
mid_col = top_left[1] + ((bottom_right[1] - top_left[1]) // 2)
val = matrix[mid_row][mid_col]
if target < val:
# We want to search from the current top-left to the current (mid_row, mid_col).
return searchRange(top_left, (mid_row, mid_col))
elif target == val:
return True
else:
# We want to search two ranges, to the right and below the current row taking care not to go out of bounds.
if mid_row < len(matrix) - 1 and mid_col < len(matrix[0]) - 1:
return searchRange((top_left[0], mid_col), (mid_row, bottom_right[1])) or searchRange(
(mid_row + 1, top_left[1]), bottom_right
)
elif mid_row < len(matrix) - 1:
return searchRange((top_left[0], mid_col), (mid_row, bottom_right[1]))
elif mid_col < len(matrix[0]) - 1:
return searchRange((mid_row + 1, top_left[1]), bottom_right)
return False
# Initial call, search the entire matrix.
return searchRange((0, 0), (len(matrix) - 1, len(matrix[0]) - 1))
def test():
executors = [BinarySearch2D, LinearSearch, MixedSearch]
tests = [
[[[1, 1]], 0, False],
[[[1, 1]], 1, True],
[[[1]], 1, True],
[[[2]], 1, False],
[
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30],
],
5,
True,
],
[
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30],
],
2,
True,
],
[
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30],
],
1,
True,
],
[
[
[1, 4, 7, 11],
[2, 5, 8, 12],
[3, 6, 9, 16],
[10, 13, 14, 17],
[18, 21, 23, 26],
],
17,
True,
],
[
[
[1, 4, 7, 11],
[2, 5, 8, 12],
[3, 6, 9, 16],
[10, 13, 14, 17],
[18, 21, 23, 26],
],
14,
True,
],
[
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30],
],
20,
False,
],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(int(float("1"))):
for col, t in enumerate(tests):
sol = executor()
result = sol.searchMatrix(t[0], t[1])
exp = t[2]
assert (
result == exp
), f"\033[93m» {result} <> {exp}\033[91m for test {col} using \033[1m{executor.__name__}"
stop = timeit.default_timer()
used = str(round(stop - start, 5))
res = "{0:20}{1:10}{2:10}".format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()