-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsearch.py
43 lines (35 loc) · 1.5 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
from typing import List
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
def binary_search(row: List[int]) -> bool:
"""Perform binary search for the target within a row."""
left, right = 0, len(row) - 1
while left <= right:
mid = left + (right - left) // 2
if row[mid] == target:
return True
elif row[mid] < target:
left = mid + 1
else:
right = mid - 1
return False
# Iterate through each row in the matrix.
for row in matrix:
# If target is within the first and last element of the row, perform binary search.
if row[0] <= target <= row[-1]:
if binary_search(row):
return True
return False
if __name__ == "__main__":
s = Solution()
# Test cases
test_cases = [
({"matrix": [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], "target": 3}, True),
({"matrix": [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], "target": 13}, False),
({"matrix": [[1]], "target": 1}, True),
({"matrix": [[1]], "target": 2}, False)
]
for i, (test_input, expected_output) in enumerate(test_cases):
result = s.searchMatrix(**test_input)
assert result == expected_output, f"Test case {i} failed: expected {expected_output}, got {result}"
print(f"Test case {i} succeeded")