-
Notifications
You must be signed in to change notification settings - Fork 43
/
search-a-2d-matrix.py
200 lines (171 loc) · 5.63 KB
/
search-a-2d-matrix.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
"""
74. Search a 2D Matrix
Medium
Write an efficient algorithm that searches for a value in an m x n 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.
Example 1:
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true
Example 2:
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 100
-104 <= matrix[i][j], target <= 104
"""
# V0
# IDEA : MATRIX IN ORDER + BRUTE FORCE
# Space: O(1)
# Time: O(m+n) # worst case
class Solution:
def searchMatrix(self, matrix, target):
if len(matrix) == 0:
return False
row, col = 0, len(matrix[0]) - 1
while row < len(matrix) and col >= 0:
if matrix[row][col] == target:
return True
elif matrix[row][col] < target:
row += 1
elif matrix[row][col] > target:
col -= 1
return False
# V0'
# IDEA : DFS
class Solution(object):
def searchMatrix(self, matrix, target):
def dfs(matrix, target, x, y):
if matrix[y][x] == target:
res.append(True)
matrix[y][x] = "#"
moves = [[0,1],[0,-1],[1,0],[-1,0]]
for move in moves:
_x = x + move[1]
_y = y + move[0]
#print ("_x = " + str(_x) + " _y = " + str(_y))
if 0 <= _x < w and 0 <= _y < l:
if matrix[_y][_x] != "#":
dfs(matrix, target, _x, _y)
if not matrix:
return False
l = len(matrix)
w = len(matrix[0])
res = []
dfs(matrix, target, 0, 0)
return True in res
# V0'
# IDEA : BINARY SEARCH
# Space: O(1)
# Time: O(logm + logn)
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix:
return False
m, n = len(matrix), len(matrix[0])
left, right = 0, m * n
while left < right:
mid = left + (right - left) / 2
if matrix[int(mid / n)][int(mid % n)]>= target:
right = mid
else:
left = mid + 1
return left < m * n and matrix[int(left / n)][int(left % n)] == target
# V1
# https://leetcode.com/problems/search-a-2d-matrix/discuss/351404/Python-Simple-Solution
class Solution:
def searchMatrix(self, matrix, target):
if len(matrix) == 0:
return False
row, col = 0, len(matrix[0]) - 1
while row < len(matrix) and col >= 0:
if matrix[row][col] == target: return True
elif matrix[row][col] < target: row += 1
elif matrix[row][col] > target: col -= 1
return False
### Test case
s=Solution()
assert s.searchMatrix([[1,2,3],[4,5,6],[7,8,9]], 9) == True
assert s.searchMatrix([[1,2,3],[4,5,6],[7,8,9]], 1) == True
assert s.searchMatrix([[1,2,3],[4,5,6],[7,8,9]], 99) == False
assert s.searchMatrix([[]], 0) == False
assert s.searchMatrix([[]], 100) == False
assert s.searchMatrix([], 100) == False
assert s.searchMatrix([[-1,3,4,-4]], -1) == False
assert s.searchMatrix([[_ for _ in range(3)] for _ in range(4)], -1) == False
assert s.searchMatrix([[_ for _ in range(3)] for _ in range(4)], 2) == True
assert s.searchMatrix([[_ for _ in range(99)] for _ in range(999)], 2) == True
# V1'
# https://leetcode.com/problems/search-a-2d-matrix/discuss/592696/python-super-easy
# IDEA : BRUTE FORCE
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
for x in matrix:
if target in x:
return True
return False
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/79459314
# https://blog.csdn.net/fuxuemingzhu/article/details/79459200
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix or not matrix[0]:
return False
rows = len(matrix)
cols = len(matrix[0])
row, col = 0, cols - 1
while True:
if row < rows and col >= 0:
if matrix[row][col] == target:
return True
elif matrix[row][col] < target:
row += 1
else:
col -= 1
else:
return False
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79459314
# https://blog.csdn.net/fuxuemingzhu/article/details/79459200
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
return any(target in row for row in matrix)
# V2
# IDEA : BINARY SEARCH
# Space: O(1)
# Time: O(logm + logn)
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if not matrix:
return False
m, n = len(matrix), len(matrix[0])
left, right = 0, m * n
while left < right:
mid = left + (right - left) / 2
if matrix[int(mid / n)][int(mid % n)]>= target:
right = mid
else:
left = mid + 1
return left < m * n and matrix[int(left / n)][int(left % n)] == target