[LeetCode] 1582. Special Positions in a Binary Matrix

Chobby·2025년 9월 24일
1

LeetCode

목록 보기
553/582

😎풀이

  1. mat를 전체 순회
    1-1. 현재 셀이 0이라면, 무시
    1-2. 현재 셀이 1이라면, 해당 열과 행에 1의 수를 증가
  2. mat를 전체 재순회
    2-1. 현재 셀이 0이라면, 무시
    2-2. 현재 셀이 1이라면, 같은 행과 열에 단 하나의 1만 있었는지 검증
    2-3. 단 하나의 1이라면, specials 증가
  3. specials의 수 반환환
function numSpecial(mat: number[][]): number {
    let specials = 0
    const n = mat.length
    const m = mat[0].length
    const rows = Array(n).fill(0)
    const cols = Array(m).fill(0)
    for(let row = 0; row < n; row++) {
        for(let col = 0; col < m; col++) {
            if(!mat[row][col]) continue
            rows[row]++
            cols[col]++
        }
    }
    for(let row = 0; row < n; row++) {
        for(let col = 0; col < m; col++) {
            if(!mat[row][col]) continue
            if(rows[row] > 1) continue
            if(cols[col] > 1) continue
            specials++
        }
    }
    return specials
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글