From 459aabb692c96be22038105c59edf0d4c6a0412d Mon Sep 17 00:00:00 2001 From: reeseo3o Date: Sat, 2 May 2026 09:11:54 +0900 Subject: [PATCH 1/2] week9: linked-list-cycle --- linked-list-cycle/reeseo3o.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 linked-list-cycle/reeseo3o.js diff --git a/linked-list-cycle/reeseo3o.js b/linked-list-cycle/reeseo3o.js new file mode 100644 index 000000000..37a0bc2bd --- /dev/null +++ b/linked-list-cycle/reeseo3o.js @@ -0,0 +1,22 @@ +// Time Complexity: O(n) +// Space Complexity: O(1) + +const hasCycle = (head) => { + if (head === null || head.next === null) { + return false; + } + + let slow = head; + let fast = head.next; + + while (slow !== fast) { + if (fast === null || fast.next === null) { + return false; + } + slow = slow.next; + fast = fast.next.next; + } + + return true; +}; + From c121c855c84d0eb66f0b1b9a1ec68e635cc02204 Mon Sep 17 00:00:00 2001 From: reeseo3o Date: Sat, 2 May 2026 09:14:50 +0900 Subject: [PATCH 2/2] week9: pacific-atlantic-water-flow --- pacific-atlantic-water-flow/reeseo3o.js | 49 +++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 pacific-atlantic-water-flow/reeseo3o.js diff --git a/pacific-atlantic-water-flow/reeseo3o.js b/pacific-atlantic-water-flow/reeseo3o.js new file mode 100644 index 000000000..4b2730b7f --- /dev/null +++ b/pacific-atlantic-water-flow/reeseo3o.js @@ -0,0 +1,49 @@ +// Time Complexity: O(m * n) +// Space Complexity: O(m * n) + +const pacificAtlantic = (heights) => { + if (!heights?.length || !heights[0]?.length) { + return []; + } + + const m = heights.length; + const n = heights[0].length; + const pacific = Array.from({ length: m }, () => Array(n).fill(false)); + const atlantic = Array.from({ length: m }, () => Array(n).fill(false)); + const dirs = [ + [-1, 0], + [1, 0], + [0, -1], + [0, 1], + ]; + + const dfs = (visited, r, c) => { + visited[r][c] = true; + for (const [dr, dc] of dirs) { + const nr = r + dr; + const nc = c + dc; + if (nr < 0 || nr >= m || nc < 0 || nc >= n || visited[nr][nc]) continue; + if (heights[nr][nc] < heights[r][c]) continue; + dfs(visited, nr, nc); + } + }; + + for (let i = 0; i < m; i++) { + dfs(pacific, i, 0); + dfs(atlantic, i, n - 1); + } + for (let j = 0; j < n; j++) { + dfs(pacific, 0, j); + dfs(atlantic, m - 1, j); + } + + const result = []; + for (let r = 0; r < m; r++) { + for (let c = 0; c < n; c++) { + if (pacific[r][c] && atlantic[r][c]) { + result.push([r, c]); + } + } + } + return result; +};