파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 7. 24. 23:45

https://leetcode.com/problems/search-a-2d-matrix-ii/

 

Search a 2D Matrix II - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

오랫만에 내 손으로 풀어봄.
무식하게 x 축으로 한번 쭉 순회하고, y 축으로 한번 쭉 순회하면 된다.

class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        found = False
        xmax, ymax = -(10**9) - 1, -(10**9) - 1
        
        for y in range(len(matrix)):
            for x in range(len(matrix[0])):
                if matrix[y][x] == target:
                    found = True                
                xmax = xmax if xmax > matrix[y][x] else matrix[y][x]
                if x == 0:
                    continue
                if matrix[y][x-1] > matrix[y][x]:
                    return False
                
        for x in range(len(matrix[0])):
            for y in range(len(matrix)):
                ymax = ymax if ymax > matrix[y][x] else matrix[y][x]
                if y == 0:
                    continue
                if matrix[y-1][x] > matrix[y][x]:
                    return False
        
        return found
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.07.26
Today's Challenge  (0) 2022.07.25
Today's Challenge  (0) 2022.07.23
Today's Challenge  (0) 2022.07.22
Today's Challenge  (0) 2022.07.21
Comments