KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • Level 1
  • C
  • 그래프
  • codeup
  • Level 2
  • js
  • 문자열
  • 제코베 JS 100제
  • 다이나믹 프로그래밍
  • Level1
  • LeetCode
  • recursion
  • 백준
  • programmers
  • C++
  • tree
  • 수학
  • Level 0
  • string
  • 정렬

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

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

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;
};
저작자표시

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Binary Search / 1608번 / Special Array With X Elements Greater Than or Equal X / JS  (1) 2024.05.28
LeetCode / Array / 661번 / Image Smoother / JS  (0) 2023.12.19
LeetCode / Array / 2090번 / K Radius Subarray Averages / JS  (0) 2023.06.21
LeetCode / Array / 1732번 / Find the Highest Altitude / JS  (0) 2023.06.21
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Binary Search / 1608번 / Special Array With X Elements Greater Than or Equal X / JS
    • LeetCode / Array / 661번 / Image Smoother / JS
    • LeetCode / Array / 2090번 / K Radius Subarray Averages / JS
    • LeetCode / Array / 1732번 / Find the Highest Altitude / JS
    KimMinJun
    KimMinJun

    티스토리툴바