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)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun
PS/LeetCode

LeetCode / Simulation / 1706번 / Where Will the Ball Fall / JS

PS/LeetCode

LeetCode / Simulation / 1706번 / Where Will the Ball Fall / JS

2023. 4. 20. 00:49

< 문제 바로가기 >

 

Where Will the Ball Fall - LeetCode

Can you solve this real interview question? Where Will the Ball Fall - You have a 2-D grid of size m x n representing a box, and you have n balls. The box is open on the top and bottom sides. Each cell in the box has a diagonal board spanning two corners o

leetcode.com

 

< 문제 간단설명 >

문제에 있는 그림대로 공이 이동했을 경우 각 열마다의 공이 몇번째 열로 마지막에 나오는지 반환하는 문제이다. 만약 중간에 길이 막혀서 갇힌다면 -1을 반환한다.

 

/**
 * @param {number[][]} grid
 * @return {number[]}
 */
var findBall = function (grid) {
  let [totalRow, totalCol] = [grid.length - 1, grid[0].length - 1];
  let result = Array.from({ length: totalCol + 1 }, () => -1);

  const DFS = (row, col) => {
    let [prevRow, nextRow] = [row - 1, row + 1];
    let [prevCol, nextCol] = [col - 1, col + 1];
    let prev = grid[row][col - 1];
    let cur = grid[row][col];
    let next = grid[row][col + 1];

    // 오른쪽 아래로 길이 나있는데, 오른쪽 열이 왼쪽 아래로 길이 나있어서 갇힌 경우
    if (cur === 1 && next === -1) {
      return -1;
    }
    // 왼쪽 아래로 길이 나있는데, 왼쪽 열이 오른쪽 아래로 길이 나있어서 갇힌 경우
    if (cur === -1 && prev === 1) {
      return -1;
    }

    // 오른쪽 아래로 길이 나있는데, 오른쪽이 벽일 경우
    if (cur === 1 && nextCol > totalCol) {
      return -1;
    }
    // 왼쪽 아래로 길이 나있는데, 왼쪽이 벽일 경우
    if (cur === -1 && prevCol < 0) {
      return -1;
    }

    // 마지막 열까지 도달했을 경우
    if (row === totalRow) {
      // 길이 오른쪽 아래로 나있을 경우
      if (cur === 1) {
        // 오른쪽으로 이동
        return col + 1;
      }
      // 길이 왼쪽 아래로 나있을 경우
      if (cur === -1) {
        // 왼쪽으로 이동
        return col - 1;
      }
    }

    // 오른쪽 아래로 이동
    if (cur === 1) {
      return DFS(nextRow, nextCol);
    }
    // 왼쪽 아래로 이동
    if (cur === -1) {
      return DFS(nextRow, prevCol);
    }
  };

  result = result.map((_, col) => {
    return DFS(0, col);
  });

  return result;
};

 

저작자표시 (새창열림)

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

LeetCode / Linked List / 148번 / Sort List / JS  (0) 2023.04.28
LeetCode / Linked List / 328번 / Odd Even Linked List / JS  (0) 2023.04.28
LeetCode / Linked List / 234번 / Palindrome Linked List / JS  (0) 2023.04.19
LeetCode / Linked List / 19번 / Remove Nth Node From End of List / JS  (0) 2023.04.19
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Linked List / 148번 / Sort List / JS
    • LeetCode / Linked List / 328번 / Odd Even Linked List / JS
    • LeetCode / Linked List / 234번 / Palindrome Linked List / JS
    • LeetCode / Linked List / 19번 / Remove Nth Node From End of List / JS
    KimMinJun
    KimMinJun

    티스토리툴바

    단축키

    내 블로그

    내 블로그 - 관리자 홈 전환
    Q
    Q
    새 글 쓰기
    W
    W

    블로그 게시글

    글 수정 (권한 있는 경우)
    E
    E
    댓글 영역으로 이동
    C
    C

    모든 영역

    이 페이지의 URL 복사
    S
    S
    맨 위로 이동
    T
    T
    티스토리 홈 이동
    H
    H
    단축키 안내
    Shift + /
    ⇧ + /

    * 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.