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)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

LeetCode / Graph / 417번 / Pacific Atlantic Water Flow / JS
PS/LeetCode

LeetCode / Graph / 417번 / Pacific Atlantic Water Flow / JS

2023. 4. 29. 18:10

< 문제 바로가기 >

 

Pacific Atlantic Water Flow - LeetCode

Can you solve this real interview question? Pacific Atlantic Water Flow - There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches

leetcode.com

 

< 문제 간단설명 >

각 칸에 있는 숫자는 그 땅의 높이라고 생각하면 된다.

따라서 그 칸에 있는 물은 현재 있는 칸의 높이보다 작거나 같은 칸으로 흐를 수 있다.

 

이 list에 위쪽과 왼쪽은 pacific ocean이고, 오른쪽과 아래쪽은 atlantic ocean이다.

두 대양 모두로 흐를 수 있는 칸일때, 그 칸의 위치를 모두 반환하면 된다.

 

/**
 * @param {number[][]} heights
 * @return {number[][]}
 */
var pacificAtlantic = function (heights) {
  const [LIST_ROW, LIST_COL] = [heights.length, heights[0].length];
  const DIR_OBJ = {
    UP: [-1, 0],
    DOWN: [1, 0],
    LEFT: [0, -1],
    RIGHT: [0, 1],
  };

  // pacific ocean 으로 흐를 수 있는 칸을 저장하는 배열
  let pacificOcean = Array.from({ length: LIST_ROW }, () =>
    Array.from({ length: LIST_COL }, () => false)
  );
  // atlantic ocean 으로 흐를 수 있는 칸을 저장하는 배열
  let atlanticOcean = Array.from({ length: LIST_ROW }, () =>
    Array.from({ length: LIST_COL }, () => false)
  );

  const isValidIndex = (row, col) => {
    if (row < 0 || row >= LIST_ROW) {
      return false;
    }
    if (col < 0 || col >= LIST_COL) {
      return false;
    }

    return true;
  };

  /**
   *
   * @param {number} prev 전의 높이
   * @param {[number, number]} cur 현재 높이의 좌표
   * @param {boolean[]} visited 방문 체크할 배열
   * @returns
   */
  const DFS = (prev, cur, visited) => {
    const [CUR_ROW, CUR_COL] = cur;

    const HEIGHT_PREV = prev;

    // 현재 행과 열이 범위를 벗어난다면
    if (isValidIndex(CUR_ROW, CUR_COL) === false) {
      return;
    }

    // 현재 위치가 이미 방문한적이 있다면
    if (visited[CUR_ROW][CUR_COL] === true) {
      return;
    }

    const HEIGHT_CUR = heights[CUR_ROW][CUR_COL];
    // 대양과 접하는 모서리에서 시작했기 때문에,
    // 현재의 높이가 전의 높이 이상이어야 대양으로 흐를 수 있음
    if (HEIGHT_CUR < HEIGHT_PREV) {
      return;
    }

    // 방문처리
    visited[CUR_ROW][CUR_COL] = true;

    for (const [MOVE_ROW, MOVE_COL] of Object.values(DIR_OBJ)) {
      const NEXT_ROW = CUR_ROW + MOVE_ROW;
      const NEXT_COL = CUR_COL + MOVE_COL;

      DFS(HEIGHT_CUR, [NEXT_ROW, NEXT_COL], visited);
    }
  };

  for (let row = 0; row < LIST_ROW; row += 1) {
    // pacific ocean과 맞닿아있는 가장 첫 열
    DFS(-Infinity, [row, 0], pacificOcean);
    // atlantic ocean과 맞닿아있는 가장 마지막 열
    DFS(-Infinity, [row, LIST_COL - 1], atlanticOcean);
  }
  for (let col = 0; col < LIST_COL; col += 1) {
    // pacific ocean과 맞닿아있는 가장 첫 행
    DFS(-Infinity, [0, col], pacificOcean);
    // atlantic ocean과 맞닿아있는 가장 마지막 행
    DFS(-Infinity, [LIST_ROW - 1, col], atlanticOcean);
  }

  let result = [];
  for (let i = 0; i < LIST_ROW; i += 1) {
    for (let j = 0; j < LIST_COL; j += 1) {
      // atlantic과 pacific 둘다로 흐를 수 있는 위치라면
      if (atlanticOcean[i][j] && pacificOcean[i][j]) {
        result.push([i, j]);
      }
    }
  }

  return result;
};
저작자표시

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

LeetCode / Dynamic Programming / 198번 / House Robber / JS  (0) 2023.05.14
LeetCode / Graph / 210번 / Course Schedule II / JS  (0) 2023.05.14
LeetCode / Graph / 994번 / Rotting Oranges / JS  (0) 2023.04.29
LeetCode / Binary Search Tree / 173번 / Binary Search Tree Iterator / JS  (0) 2023.04.29
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Dynamic Programming / 198번 / House Robber / JS
    • LeetCode / Graph / 210번 / Course Schedule II / JS
    • LeetCode / Graph / 994번 / Rotting Oranges / JS
    • LeetCode / Binary Search Tree / 173번 / Binary Search Tree Iterator / JS
    KimMinJun
    KimMinJun

    티스토리툴바