From 258cf2d33c201a2973207b0a0f41cd80e2265c55 Mon Sep 17 00:00:00 2001 From: JungHwan Date: Fri, 19 Jul 2024 12:07:29 +0900 Subject: [PATCH] 7/19 1Q --- JungHwan/Day19/NeetCode_Search2DMatrix.py | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 JungHwan/Day19/NeetCode_Search2DMatrix.py diff --git a/JungHwan/Day19/NeetCode_Search2DMatrix.py b/JungHwan/Day19/NeetCode_Search2DMatrix.py new file mode 100644 index 0000000..c0aa653 --- /dev/null +++ b/JungHwan/Day19/NeetCode_Search2DMatrix.py @@ -0,0 +1,34 @@ +class Solution: + def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: + + + #first search + colstart, colend = 0, len(matrix) - 1 + while colstart < colend: + half = int((colstart + colend) / 2) + print(matrix[half][0] , target) + if matrix[half][0] < target: + colstart = half + 1 + elif matrix[half][0] > target: + colend = half - 1 + else: + return True + if matrix[colstart][0] == matrix[colend][0] == target: + return True + if matrix[colstart][0] > target: + colstart -= 1 + + #second search + rowstart, rowend = 0, len(matrix[0]) - 1 + while rowstart < rowend: + half = int((rowstart + rowend) / 2) + print(matrix[colstart][half] , target) + if matrix[colstart][half] < target: + rowstart = half + 1 + elif matrix[colstart][half] > target: + rowend = half - 1 + else: + return True + if matrix[colstart][rowstart] == matrix[colstart][rowend] == target: + return True + return False \ No newline at end of file