Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions JungHwan/Day19/NeetCode_Search2DMatrix.py
Original file line number Diff line number Diff line change
@@ -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