소프트웨어에 대한 모든 것

LeetCode 풀기 - 1380. Lucky Numbers in a Matrix 본문

알고리즘/LeetCode

LeetCode 풀기 - 1380. Lucky Numbers in a Matrix

앤테바 2021. 12. 29. 20:01
반응형

1380. Lucky Numbers in a Matrix

https://leetcode.com/problems/lucky-numbers-in-a-matrix/

 

Lucky Numbers in a Matrix - 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

문제)

솔루션1)

class Solution:
    def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
        n_row = len(matrix)
        n_col = len(matrix[0])
        
        mins = []
        for i in range(n_row):
            mins.append(min(matrix[i]))    
            
        matrix = list(zip(*matrix))
        
        maxs = []
        for i in range(n_col):
            maxs.append(max(matrix[i]))
        
        return list(set(mins) & set(maxs))

솔루션2)

솔루션1 코드 축약 버전

class Solution:
    def luckyNumbers (self, matrix: List[List[int]]) -> List[int]:
        mins = {min(row) for row in matrix}
        maxs = {max(col) for col in zip(*matrix)}
        return mins & maxs

 

반응형
Comments