Skip to content

Commit

Permalink
Merge pull request #981 from jylim-dev/main
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Jul 22, 2024
2 parents df8f044 + def43c6 commit 6569cb7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Jiyoung/133. Clone Graph.js
Original file line number Diff line number Diff line change
@@ -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;
};
30 changes: 30 additions & 0 deletions Jiyoung/417. Pacific Atlantic Water Flow.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 6569cb7

Please sign in to comment.