Published:
Updated:

  • 이런 λ¬Έμ œκ°€ λ‚˜μ˜€λ©΄ 컬럼의 λ’€λΆ€ν„° 탐색할 μ‹œμ— κ°•λ ₯ν•œ 이점이 있음


Solution

from typing import List


class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        # λ’€λΆ€ν„° νƒμƒ‰ν•˜λŠ” 것이 더 νš¨μœ¨μ μž„
        row, col = 0, len(matrix[0]) - 1

        while row < len(matrix) and col >= 0:
            current = matrix[row][col]

            if current == target:
                return True

            # λ’€λΆ€ν„° νƒμƒ‰ν•˜λ‹ˆκΉŒ λ°”λ‘œ λ‹€μŒ row둜 λ„˜μ–΄κ°ˆ 수 μžˆλŠ” κ±°μž„
            if current < target:
                row += 1
                continue

            # 찾을 κ°€λŠ₯성이 μžˆμœΌλ‹ˆ μ™Όμͺ½μœΌλ‘œ κ°€λ©΄μ„œ 탐색
            if current > target:
                col -= 1

        return False


Reference

Leave a comment