Skip to content

Latest commit

 

History

History
50 lines (35 loc) · 1.21 KB

_74. Search a 2D Matrix.md

File metadata and controls

50 lines (35 loc) · 1.21 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 15, 2024

Last updated : June 15, 2024


Related Topics : Array, Binary Search, Matrix

Acceptance Rate : 51.55 %


Solutions

Python

class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        left, right = 0, len(matrix) * len(matrix[0]) - 1

        while left < right :
            mid = (left + right) // 2
            row = mid // len(matrix[0])
            col = mid % len(matrix[0])

            if matrix[row][col] == target :
                return True
            if matrix[row][col] < target :
                left = mid + 1
            else :
                right = mid
            
        mid = (left + right) // 2
        row = mid // len(matrix[0])
        col = mid % len(matrix[0])

        return matrix[row][col] == target