From a87863cacb620e8bef245ace5d51f484994f0e38 Mon Sep 17 00:00:00 2001 From: Rodolfo P A <6721075+rodoufu@users.noreply.github.com> Date: Mon, 13 Jul 2026 15:04:14 -0300 Subject: [PATCH] Create search-a-2d-matrix-2.py --- leetCode/matrix/search-a-2d-matrix-2.py | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 leetCode/matrix/search-a-2d-matrix-2.py diff --git a/leetCode/matrix/search-a-2d-matrix-2.py b/leetCode/matrix/search-a-2d-matrix-2.py new file mode 100644 index 0000000..a7c5390 --- /dev/null +++ b/leetCode/matrix/search-a-2d-matrix-2.py @@ -0,0 +1,32 @@ +# https://leetcode.com/problems/search-a-2d-matrix/ +import bisect + +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + if len(matrix) == 0 or len(matrix[0]) == 0: + return False + + low_l = 0 + high_l = len(matrix) - 1 + + while low_l <= high_l: + mid_l = low_l + (high_l - low_l) // 2 + + if matrix[mid_l][0] == target: + return True + elif matrix[mid_l][0] < target: + if mid_l + 1 >= len(matrix) or matrix[mid_l + 1][0] > target: + break + low_l = mid_l + 1 + else: + high_l = mid_l - 1 + + if mid_l >= len(matrix): + return False + + # print(f"mid_l: {mid_l}") + pos = bisect.bisect_left(matrix[mid_l], target) + # print(f"mid_l: {mid_l}, pos: {pos}") + if pos >= len(matrix[0]): + return False + return matrix[mid_l][pos] == target