From d819cc55929ba1bef15a26a87c3d841e3d50b00f Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Thu, 19 Sep 2024 05:53:44 +0900 Subject: [PATCH 1/6] 2. Longest Substring Without Repeating Characters --- .../sunjae95.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 longest-substring-without-repeating-characters/sunjae95.js diff --git a/longest-substring-without-repeating-characters/sunjae95.js b/longest-substring-without-repeating-characters/sunjae95.js new file mode 100644 index 000000000..d4aad854a --- /dev/null +++ b/longest-substring-without-repeating-characters/sunjae95.js @@ -0,0 +1,23 @@ +/** + * @description + * brainstorming: + * hash table + two pointer + * + * n = length of s + * time complexity: O(n) + * space complexity: O(n) + */ +var lengthOfLongestSubstring = function (s) { + const map = new Map(); + let answer = 0; + let start = 0; + let end = 0; + for (let i = 0; i < s.length; i++) { + if (map.has(s[i])) start = Math.max(map.get(s[i]) + 1, start); + map.set(s[i], i); + end += 1; + answer = Math.max(answer, end - start); + } + + return answer; +}; From f8fa89adb05a5473d44a6a3b069daba7362bb32b Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Thu, 19 Sep 2024 06:27:35 +0900 Subject: [PATCH 2/6] 3. Number of Islands --- number-of-islands/sunjae95.js | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 number-of-islands/sunjae95.js diff --git a/number-of-islands/sunjae95.js b/number-of-islands/sunjae95.js new file mode 100644 index 000000000..fb5398020 --- /dev/null +++ b/number-of-islands/sunjae95.js @@ -0,0 +1,49 @@ +/** + * @description + * brainstorming: + * hash table + two pointer + * + * n = length of grid + * k = length of grid[index] + * time complexity: O(n * k) + * space complexity: O(n * k) + */ +var numIslands = function (grid) { + let answer = 0; + const visited = Array.from({ length: grid.length }, (_, i) => + Array.from({ length: grid[i].length }, () => false) + ); + + const dfs = (r, c) => { + const dr = [0, 1, 0, -1]; + const dc = [1, 0, -1, 0]; + + for (let i = 0; i < 4; i++) { + const nextR = r + dr[i]; + const nextC = c + dc[i]; + + if ( + nextR >= 0 && + nextR < grid.length && + nextC >= 0 && + nextC < grid[r].length && + grid[nextR][nextC] == 1 && + !visited[nextR][nextC] + ) { + visited[nextR][nextC] = true; + dfs(nextR, nextC); + } + } + }; + + for (let row = 0; row < grid.length; row++) { + for (let column = 0; column < grid[row].length; column++) { + if (grid[row][column] == 1 && !visited[row][column]) { + visited[row][column] = true; + answer++; + dfs(row, column); + } + } + } + return answer; +}; From 47559859193898dd58ccf2066a3f4fb36b01fc11 Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Thu, 19 Sep 2024 16:16:59 +0900 Subject: [PATCH 3/6] 4. Unique Paths --- unique-paths/sunjae95.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 unique-paths/sunjae95.js diff --git a/unique-paths/sunjae95.js b/unique-paths/sunjae95.js new file mode 100644 index 000000000..1c2e0ce77 --- /dev/null +++ b/unique-paths/sunjae95.js @@ -0,0 +1,25 @@ +/** + * @description + * brainstorming: + * 1. dfs -> time limited + * 2. dynamic programming + * + * time complexity: O(m * n) + * space complexity: O(m * n) + */ + +var uniquePaths = function (m, n) { + // initialize + const dp = Array.from({ length: m }, (_, i) => + Array.from({ length: n }, (_, j) => (i === 0 || j === 0 ? 1 : 0)) + ); + + for (let i = 1; i < m; i++) { + for (let j = 1; j < n; j++) { + // recurrence relation + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; + } + } + + return dp[m - 1][n - 1]; +}; From d6e389086138443bbc1f32fa46b9b7e52b04b713 Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Tue, 24 Sep 2024 14:27:29 +0900 Subject: [PATCH 4/6] 5. Set Matrix Zeroes --- set-matrix-zeroes/sunjae95.js | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 set-matrix-zeroes/sunjae95.js diff --git a/set-matrix-zeroes/sunjae95.js b/set-matrix-zeroes/sunjae95.js new file mode 100644 index 000000000..23d327098 --- /dev/null +++ b/set-matrix-zeroes/sunjae95.js @@ -0,0 +1,46 @@ +/** + * @description + * brainstorming: + * memoization + * + * m: length of matrix + * n: length of matrix[i] + * time complexity: O(m * n) + * space complexity: O(m * n) + */ +var setZeroes = function (matrix) { + const stack = []; + const memo = { row: new Set(), column: new Set() }; + + const setZero = ({ r, c, isRow, isColumn }) => { + const length = isRow ? matrix.length : matrix[0].length; + + for (let i = 0; i < length; i++) { + const row = isRow ? i : r; + const column = isColumn ? i : c; + matrix[row][column] = 0; + } + }; + + matrix.forEach((row, r) => { + row.forEach((value, c) => { + if (value === 0) stack.push([r, c]); + }); + }); + + while (stack.length) { + const [r, c] = stack.pop(); + + if (!memo.row.has(r)) { + setZero({ r, c, isColumn: true }); + memo.row.add(r); + } + + if (!memo.column.has(c)) { + setZero({ r, c, isRow: true }); + memo.column.add(c); + } + } + + return matrix; +}; From 938f41563bfd9c154523f39db81590080b7d6dbd Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Thu, 19 Sep 2024 02:43:24 +0900 Subject: [PATCH 5/6] 1. Reverse Linked List --- reverse-linked-list/sunjae95.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 reverse-linked-list/sunjae95.js diff --git a/reverse-linked-list/sunjae95.js b/reverse-linked-list/sunjae95.js new file mode 100644 index 000000000..1042f9486 --- /dev/null +++ b/reverse-linked-list/sunjae95.js @@ -0,0 +1,25 @@ +/** + * @description + * brainstorming: + * Thinking of stacking nodes like stacks while traveling + * + * time complexity: O(n) + * space complexity: O(n) + */ + +var reverseList = function (head) { + let answer = null; + + const search = (target) => { + if (target === null) return; + + const node = new ListNode(target.val, answer); + answer = node; + + search(target.next); + }; + + search(head); + + return answer; +}; From 10d40b1b667ef2b87be2943045927e6fc4373e1d Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Sat, 28 Sep 2024 20:21:32 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=EB=A6=AC=EB=B7=B0=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- longest-substring-without-repeating-characters/sunjae95.js | 5 ++--- reverse-linked-list/sunjae95.js | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/longest-substring-without-repeating-characters/sunjae95.js b/longest-substring-without-repeating-characters/sunjae95.js index d4aad854a..be1fb40c4 100644 --- a/longest-substring-without-repeating-characters/sunjae95.js +++ b/longest-substring-without-repeating-characters/sunjae95.js @@ -11,12 +11,11 @@ var lengthOfLongestSubstring = function (s) { const map = new Map(); let answer = 0; let start = 0; - let end = 0; + for (let i = 0; i < s.length; i++) { if (map.has(s[i])) start = Math.max(map.get(s[i]) + 1, start); map.set(s[i], i); - end += 1; - answer = Math.max(answer, end - start); + answer = Math.max(answer, i - start + 1); } return answer; diff --git a/reverse-linked-list/sunjae95.js b/reverse-linked-list/sunjae95.js index 1042f9486..84706d23f 100644 --- a/reverse-linked-list/sunjae95.js +++ b/reverse-linked-list/sunjae95.js @@ -10,16 +10,16 @@ var reverseList = function (head) { let answer = null; - const search = (target) => { + const buildReverseList = (target) => { if (target === null) return; const node = new ListNode(target.val, answer); answer = node; - search(target.next); + buildReverseList(target.next); }; - search(head); + buildReverseList(head); return answer; };