PS/LeetCode

LeetCode / Array / 1582번 / Special Positions in a Binary Matrix / JS

KimMinJun 2023. 12. 13. 15:39

< 문제 바로가기 >

 

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

 

< 문제 간단설명 >

2차원 배열에서 (i, j)의 값이 1이라면, 같은 행과 같은 열에 1이 (i, j)에만 존재했을 때의 개수를 구하는 문제이다.

 

 

/**
 * @param {number[][]} mat
 * @return {number}
 */
const numSpecial = (mat) => {
  let result = 0;

  const row = mat.length;
  const col = mat[0].length;

  /**
   * 해당 행에 1이 한개만 있는지 확인하는 함수
   *
   * @param {number} r 행
   * @returns 해당 행에 1이 한개만 있다면 true, 아니라면 false를 반환한다.
   */
  const checkRow = (r) => {
    let oneCount = 0;

    for (let j = 0; j < col; j++) {
      if (mat[r][j] === 1) {
        oneCount++;
      }
    }

    return oneCount === 1;
  };

  /**
   * 해당 열에 1이 한개만 있는지 확인하는 함수
   *
   * @param {number} c 열
   * @returns 해당 열에 1이 한개만 있다면 true, 아니라면 false를 반환한다.
   */
  const checkCol = (c) => {
    let oneCount = 0;

    for (let i = 0; i < row; i++) {
      if (mat[i][c] === 1) {
        oneCount++;
      }
    }

    return oneCount === 1;
  };

  for (let i = 0; i < row; i++) {
    for (let j = 0; j < col; j++) {
      // 현재 칸이 1이고, 행과 열 모두 검사했을 때 1이 한개만 있다면 result 값을 더해준다.
      if (mat[i][j] === 1 && checkRow(i) && checkCol(j)) {
        result++;
      }
    }
  }

  return result;
};