파비의 매일매일 공부기록

2023.12.14 Today's Challenge 본문

Problem Solving/LeetCode

2023.12.14 Today's Challenge

fabichoi 2023. 12. 14. 23:45

https://leetcode.com/problems/special-positions-in-a-binary-matrix/

 

Special Positions in a Binary Matrix - LeetCode

Can you solve this real interview question? Special Positions in a Binary Matrix - Given an m x n binary matrix mat, return the number of special positions in mat. A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and co

leetcode.com

압축해서 count 하는 식으로 판단하면 됨

class Solution:
    def numSpecial(self, mat: List[List[int]]) -> int:
        m, n = len(mat), len(mat[0])
        r_cnt, c_cnt = [0]*m, [0]*n

        for r in range(m):
            for c in range(n):
                if mat[r][c] == 1:
                    r_cnt[r] += 1
                    c_cnt[c] += 1
        
        ans = 0

        for r in range(m):
            for c in range(n):
                if mat[r][c] == 1:
                    if r_cnt[r] == 1 and c_cnt[c] == 1:
                        ans += 1
        
        return ans
반응형

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

2023.12.16 Today's Challenge  (1) 2023.12.16
2023.12.15 Today's Challenge  (0) 2023.12.15
2023.12.13 Today's Challenge  (0) 2023.12.13
2023.12.12 Today's Challenge  (0) 2023.12.12
2023.12.11 Today's Challenge  (0) 2023.12.11
Comments