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