From 2a1025b5fb3c7e2ce79d8ef4cba79c581434f628 Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Sun, 19 Jan 2025 00:18:56 +0900 Subject: [PATCH 1/8] reverse-linked-list solution --- reverse-linked-list/jdy8739.js | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 reverse-linked-list/jdy8739.js diff --git a/reverse-linked-list/jdy8739.js b/reverse-linked-list/jdy8739.js new file mode 100644 index 000000000..83f85d354 --- /dev/null +++ b/reverse-linked-list/jdy8739.js @@ -0,0 +1,43 @@ + +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var reverseList = function(head) { + if (head === null) { + return null; + } + + if (head.next === null) { + return head; + } + + const stack = []; + + let nextNode = head; + + while (nextNode) { + stack.push(nextNode); + + nextNode = nextNode.next; + } + + for (let i=stack.length - 1; i>=0; i--) { + if (i === 0) { + stack[i].next = null; + } else { + stack[i].next = stack[i - 1]; + } + } + + return stack[stack.length - 1]; +}; + +// 시간복잡도 O(2n) From 419d34c90e47426ded51ac10accaec9cca81fc9d Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Sun, 19 Jan 2025 00:21:45 +0900 Subject: [PATCH 2/8] =?UTF-8?q?fix:=20=EA=B0=9C=ED=96=89=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reverse-linked-list/jdy8739.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/reverse-linked-list/jdy8739.js b/reverse-linked-list/jdy8739.js index 83f85d354..089ae0d69 100644 --- a/reverse-linked-list/jdy8739.js +++ b/reverse-linked-list/jdy8739.js @@ -41,3 +41,5 @@ var reverseList = function(head) { }; // 시간복잡도 O(2n) + + From 20470c3066eaeee6027f217cc29acc23ffc272ba Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Mon, 20 Jan 2025 00:10:00 +0900 Subject: [PATCH 3/8] longest-substring-without-repeating-characters solution --- .../jdy8739.js | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 longest-substring-without-repeating-characters/jdy8739.js diff --git a/longest-substring-without-repeating-characters/jdy8739.js b/longest-substring-without-repeating-characters/jdy8739.js new file mode 100644 index 000000000..471fb969c --- /dev/null +++ b/longest-substring-without-repeating-characters/jdy8739.js @@ -0,0 +1,36 @@ +/** + * @param {string} s + * @return {number} + */ +var lengthOfLongestSubstring = function(s) { + let start = 0; + let end = 0; + + const set = new Set(); + + let max = 0; + + while (end < s.length) { + const char = s[end]; + + if (set.has(char)) { + set.delete(s[start]); + + start++; + } else { + set.add(char); + + end++; + } + + max = Math.max(max, set.size); + } + + return max; +}; + +// +// + + + From c75d8e9d528916b4d59e3637b9102c93f5619bf7 Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Tue, 21 Jan 2025 00:36:08 +0900 Subject: [PATCH 4/8] set-matrix-zeroes solution --- set-matrix-zeroes/jdy8739.js | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 set-matrix-zeroes/jdy8739.js diff --git a/set-matrix-zeroes/jdy8739.js b/set-matrix-zeroes/jdy8739.js new file mode 100644 index 000000000..119dc3f72 --- /dev/null +++ b/set-matrix-zeroes/jdy8739.js @@ -0,0 +1,38 @@ +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var setZeroes = function (matrix) { + const coord = []; + + for (let i = 0; i < matrix.length; i++) { + for (let j = 0; j < matrix[i].length; j++) { + + const num = matrix[i][j]; + + if (num === 0) { + coord.push({ y: i, x: j }); + } + } + } + + for (let k = 0; k < coord.length; k++) { + const { y } = coord[k]; + + for (let j = 0; j < matrix[0].length; j++) { + matrix[y][j] = 0; + } + } + + for (let l = 0; l < coord.length; l++) { + const { x } = coord[l]; + + for (let j = 0; j < matrix.length; j++) { + matrix[j][x] = 0; + } + } +}; + +// 시간복잡도 O(n * m) +// 공간복잡도 O(n * m) + From 38f7eac81c384163071628f3a371791ba70f2596 Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Thu, 23 Jan 2025 22:53:02 +0900 Subject: [PATCH 5/8] unique-paths solution --- unique-paths/jdy8739.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 unique-paths/jdy8739.js diff --git a/unique-paths/jdy8739.js b/unique-paths/jdy8739.js new file mode 100644 index 000000000..3dd303dbd --- /dev/null +++ b/unique-paths/jdy8739.js @@ -0,0 +1,35 @@ +var uniquePaths = function (m, n) { + const cache = new Map(); + + const dfs = (row, col) => { + const cacheKey = `${row}-${col}`; + + if (cache.has(cacheKey)) { + return cache.get(cacheKey); + } + + if (row === m - 1 && col === n - 1) { + return 1; + } + + let count = 0; + + if (row < m - 1) { + count += dfs(row + 1, col); + } + + if (col < n - 1) { + count += dfs(row, col + 1); + } + + cache.set(cacheKey, count); + + return count; + } + + return dfs(0, 0); +}; + +// 시간복잡도 O(m * n) +// 공간복잡도 O(m * n) - 1 (matrix[m][n]에 대한 캐시는 포함되지 않으므로) + From 5cfe8c4940716edaa9eef7e88e304728e7ecf811 Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Fri, 24 Jan 2025 00:05:06 +0900 Subject: [PATCH 6/8] unique-paths solutions --- unique-paths/jdy8739.js | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/unique-paths/jdy8739.js b/unique-paths/jdy8739.js index 3dd303dbd..1577de0e6 100644 --- a/unique-paths/jdy8739.js +++ b/unique-paths/jdy8739.js @@ -26,10 +26,47 @@ var uniquePaths = function (m, n) { return count; } - + return dfs(0, 0); }; // 시간복잡도 O(m * n) // 공간복잡도 O(m * n) - 1 (matrix[m][n]에 대한 캐시는 포함되지 않으므로) +var uniquePaths = function(m, n) { + const matrix = []; + + for (let i=0; i Date: Sat, 25 Jan 2025 00:13:56 +0900 Subject: [PATCH 7/8] number-of-islands solution --- number-of-islands/jdy8739.js | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 number-of-islands/jdy8739.js diff --git a/number-of-islands/jdy8739.js b/number-of-islands/jdy8739.js new file mode 100644 index 000000000..bb2417382 --- /dev/null +++ b/number-of-islands/jdy8739.js @@ -0,0 +1,43 @@ +/** + * @param {character[][]} grid + * @return {number} + */ +var numIslands = function (grid) { + const sink = (row, col) => { + grid[row][col] = '0'; + + const neighbor = [ + [row + 1, col], [row, col + 1], [row - 1, col], [row, col - 1] + ].filter(([y, x]) => { + return y >= 0 && x >= 0 && y < grid.length && x < grid[0].length; + }).filter(([y, x]) => { + return grid[y][x] === '1'; + }); + + neighbor.forEach(([y, x]) => { + const el = grid[y][x]; + + if (el === '1') { + sink(y, x); + } + }) + } + + let count = 0; + + for (let i = 0; i < grid.length; i++) { + for (let j = 0; j < grid[0].length; j++) { + + if (grid[i][j] === '1') { + count++; + sink(i, j); + } + } + } + + return count; +}; + +// 시간복잡도 O(2 * m * n) -> m * n 만큼 반복 + 재귀적으로 방문할 수 있는 셀의 수는 총 m * n 개 +// 공간복잡도 O(1) -> 입력 배열을 사용하지 않고 변수만 사용 + From edb6386b7813cd73914a842f18d5c2cd4148fab1 Mon Sep 17 00:00:00 2001 From: jdy8739 Date: Sat, 25 Jan 2025 18:29:12 +0900 Subject: [PATCH 8/8] =?UTF-8?q?longest-substring-without-repeating-charact?= =?UTF-8?q?ers=20=EC=A3=BC=EC=84=9D=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- longest-substring-without-repeating-characters/jdy8739.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/longest-substring-without-repeating-characters/jdy8739.js b/longest-substring-without-repeating-characters/jdy8739.js index 471fb969c..9ab6e60f7 100644 --- a/longest-substring-without-repeating-characters/jdy8739.js +++ b/longest-substring-without-repeating-characters/jdy8739.js @@ -29,8 +29,5 @@ var lengthOfLongestSubstring = function(s) { return max; }; -// -// - - - +// 시간복잡도 O(2n) -> 최대 2n번 반복 +// 공간복잡도 O(n) -> 최대 s의 길이만큼 공간을 사용