From def43c61f11b3dfd017491196ea574fecf5b70a2 Mon Sep 17 00:00:00 2001 From: jylim-dev Date: Sat, 20 Jul 2024 17:46:21 -0700 Subject: [PATCH] week6 --- Jiyoung/133. Clone Graph.js | 11 ++++++++ Jiyoung/417. Pacific Atlantic Water Flow.js | 30 +++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 Jiyoung/133. Clone Graph.js create mode 100644 Jiyoung/417. Pacific Atlantic Water Flow.js diff --git a/Jiyoung/133. Clone Graph.js b/Jiyoung/133. Clone Graph.js new file mode 100644 index 00000000..b6fa3522 --- /dev/null +++ b/Jiyoung/133. Clone Graph.js @@ -0,0 +1,11 @@ +const clone = new Map(); +var cloneGraph = function(node) { + if (!node) return null; + if (clone.has(node)) return clone.get(node); + + const newNode = new Node(node.val); + clone.set(node, newNode); + newNode.neighbors = node.neighbors.map(x => cloneGraph(x)); + + return newNode; +}; diff --git a/Jiyoung/417. Pacific Atlantic Water Flow.js b/Jiyoung/417. Pacific Atlantic Water Flow.js new file mode 100644 index 00000000..6cc16393 --- /dev/null +++ b/Jiyoung/417. Pacific Atlantic Water Flow.js @@ -0,0 +1,30 @@ +var pacificAtlantic = function (heights) { + if (!heights.length) return heights; + let row = heights.length, col = heights[0].length, coords = []; + const peaks = new Array(row * col).fill(0); + + const findPeak = (i, j, ocean, prevHeight) => { + let peakIdx = (i * col) + j; + if (peaks[peakIdx] === ocean || peaks[peakIdx] === 3 || heights[i][j] < prevHeight) return; + peaks[peakIdx] += ocean, prevHeight = heights[i][j]; + + if (peaks[peakIdx] === 3) coords.push([i,j]); + if (i + 1 < row) findPeak(i + 1, j, ocean, prevHeight); + if (i > 0) findPeak(i - 1, j, ocean, prevHeight); + if (j + 1 < col) findPeak(i, j + 1, ocean, prevHeight); + if (j > 0) findPeak(i, j - 1, ocean, prevHeight); + } + + for (let i = 0; i < row; i++) { + findPeak(i, 0, 1, heights[i][0]); + findPeak(i, col - 1, 2, heights[i][col - 1]); + } + + for (let j = 0; j < col; j++) { + findPeak(0, j, 1, heights[0][j]); + findPeak(row - 1, j, 2, heights[row - 1][j]); + } + + return coords; + } + \ No newline at end of file