From e25b574443f24b078977a9608b08e1a8dd75256b Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Mon, 12 May 2025 13:25:23 +0900 Subject: [PATCH 1/5] add Reverse Linked List solution --- reverse-linked-list/HoonDongKang.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 reverse-linked-list/HoonDongKang.ts diff --git a/reverse-linked-list/HoonDongKang.ts b/reverse-linked-list/HoonDongKang.ts new file mode 100644 index 000000000..17521e050 --- /dev/null +++ b/reverse-linked-list/HoonDongKang.ts @@ -0,0 +1,28 @@ +/** + * [Problem]: [206] Reverse Linked List + * (https://leetcode.com/problems/reverse-linked-list/description/) + */ + +// Definition for singly-linked list. +class ListNode { + val: number; + next: ListNode | null; + constructor(val?: number, next?: ListNode | null) { + this.val = val === undefined ? 0 : val; + this.next = next === undefined ? null : next; + } +} +function reverseList(head: ListNode | null): ListNode | null { + // 시간복잡도 O(n) + // 공간복잡도 O(1) + let prev: ListNode | null = null; + let cur = head; + while (cur) { + let next = cur.next; + cur.next = prev; + prev = cur; + cur = next; + } + + return prev; +} From c8c087aa150571f0cc438d49a962726ad6883140 Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Tue, 13 May 2025 11:38:44 +0900 Subject: [PATCH 2/5] add Longest Substring Without Repeating Characters solution --- .../HoonDongKang.ts | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 longest-substring-without-repeating-characters/HoonDongKang.ts diff --git a/longest-substring-without-repeating-characters/HoonDongKang.ts b/longest-substring-without-repeating-characters/HoonDongKang.ts new file mode 100644 index 000000000..9f117ee6f --- /dev/null +++ b/longest-substring-without-repeating-characters/HoonDongKang.ts @@ -0,0 +1,80 @@ +/** + * [Problem]: [3] Longest Substring Without Repeating Characters + * (https://leetcode.com/problems/longest-substring-without-repeating-characters/description/) + */ + +/* + * 1. 중복없이 이어는 문자열 중 가장 긴 문자열 + */ +function lengthOfLongestSubstring(s: string): number { + // 시간 복잡도: O(n^2) + // 공간 복잡도: O(n) + function bruteForceFunc(s: string): number { + let max = 0; + for (let i = 0; i < s.length; i++) { + const duplicate = new Set(); + let j = i; + let count = 0; + + while (j < s.length && !duplicate.has(s[j])) { + duplicate.add(s[j]); + count++; + j++; + } + + max = Math.max(max, count); + } + + return max; + } + //시간복잡도 O(n) + //공간복잡도 O(n) + function slidingWindowFunc(s: string): number { + let left = 0; + let right = 0; + let window = new Set(); + let max = 0; + while (right < s.length) { + if (window.has(s[right])) { + window.delete(s[left]); + left++; + } else { + window.add(s[right]); + max = Math.max(max, right - left + 1); + right++; + } + // while (window.has(s[right])) { + // window.delete(s[left]); + // left++; + // } + + // window.add(s[right]); + // max = Math.max(max, right - left + 1); + // right++; + } + + return max; + } + + //시간복잡도 O(n) + //공간복잡도 O(n) + //set에서 left 인덱스를 조금 더 빠르게 가져올 수 있음 + function mapFunc(s: string): number { + let left = 0; + let max = 0; + let map = new Map(); + + for (let right = 0; right < s.length; right++) { + if (map.has(s[right])) { + left = Math.max(left, map.get(s[right])! + 1); + } + + map.set(s[right], right); + max = Math.max(max, right - left + 1); + } + + return max; + } + + return mapFunc(s); +} From 4f193b4fc91d639283346bcb72fe0b70db7963ea Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Wed, 14 May 2025 11:19:05 +0900 Subject: [PATCH 3/5] add Number of Islands solution --- number-of-islands/HoonDongKang.ts | 80 +++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 number-of-islands/HoonDongKang.ts diff --git a/number-of-islands/HoonDongKang.ts b/number-of-islands/HoonDongKang.ts new file mode 100644 index 000000000..d3c1b2e25 --- /dev/null +++ b/number-of-islands/HoonDongKang.ts @@ -0,0 +1,80 @@ +/** + * [Problem]: [200] Number of Islands + * (https://leetcode.com/problems/number-of-islands/description/) + */ +function numIslands(grid: string[][]): number { + //시간복잡도 O(n) + //공간복잡도 O(n) + function dfsFunc(grid: string[][]): number { + let rows = grid.length; + let cols = grid[0].length; + let count = 0; + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (grid[i][j] === "1") { + count++; + traveling(i, j); + } + } + } + + function traveling(i: number, j: number) { + grid[i][j] = "#"; + + [ + [i - 1, j], + [i + 1, j], + [i, j - 1], + [i, j + 1], + ].forEach(([r, c]) => { + if (0 <= r && r < rows && 0 <= c && c < cols) { + if (grid[r][c] === "1") traveling(r, c); + } + }); + } + + return count; + } + + //시간복잡도 O(n) + //공간복잡도 O(n) + function stackFunc(grid: string[][]): number { + let rows = grid.length; + let cols = grid[0].length; + let count = 0; + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (grid[i][j] === "1") { + count++; + traveling(i, j); + } + } + } + + function traveling(i: number, j: number) { + let stack = [[i, j]]; + + while (stack.length) { + let [r, c] = stack.pop()!; + grid[r][c] = "#"; + + [ + [r - 1, c], + [r + 1, c], + [r, c - 1], + [r, c + 1], + ].forEach(([r, c]) => { + if (0 <= r && r < rows && 0 <= c && c < cols) { + if (grid[r][c] === "1") stack.push([r, c]); + } + }); + } + } + + return count; + } + + return stackFunc(grid); +} From 7c253cd5dd92f0a0cbc230144a261fb39322747d Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Thu, 15 May 2025 12:40:24 +0900 Subject: [PATCH 4/5] add Unique Paths solution --- unique-paths/HoonDongKang.ts | 69 ++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 unique-paths/HoonDongKang.ts diff --git a/unique-paths/HoonDongKang.ts b/unique-paths/HoonDongKang.ts new file mode 100644 index 000000000..2f60f1e6e --- /dev/null +++ b/unique-paths/HoonDongKang.ts @@ -0,0 +1,69 @@ +/** + * [Problem]: [62] Unique Paths + * (https://leetcode.com/problems/unique-paths/description/) + */ + +function uniquePaths(m: number, n: number): number { + //시간복잡도 O(2^(m*n)) + //공간복잡도 O(m*n) + // Time Limit Exceeded + function dfsFunc(m: number, n: number): number { + let result: number = 0; + function dfs(row: number, col: number) { + if (row === m - 1 && col === n - 1) result++; + if (row + 1 < m) dfs(row + 1, col); + if (col + 1 < n) dfs(row, col + 1); + } + + dfs(0, 0); + return result; + } + + //시간복잡도 O(m*n) + //공간복잡도 O(m*n) + function memoizationFunc(m: number, n: number): number { + let memo = new Map(); + + function dfs(row: number, col: number) { + const key = `${row}|${col}`; + + if (memo.has(key)) return memo.get(key); + if (row === m - 1 && col === n - 1) return 1; + if (row >= m || col >= n) return 0; + + const result = dfs(row + 1, col) + dfs(row, col + 1); + + memo.set(key, result); + return result; + } + + return dfs(0, 0); + } + //시간복잡도 O(m*n) + //공간복잡도 O(m*n) + function dpFunc(m: number, n: number): number { + let dp: number[][] = Array.from({ length: m }, () => Array(n).fill(1)); + + for (let i = 1; i < m; i++) { + for (let j = 1; j < n; j++) { + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; + } + } + + return dp[m - 1][n - 1]; + } + //시간복잡도 O(m*n) + //공간복잡도 O(n) + function optimizationFunc(m: number, n: number): number { + let dp: number[] = new Array(n).fill(1); + for (let i = 1; i < m; i++) { + let left = 1; + for (let j = 1; j < n; j++) { + dp[j] += left; + left = dp[j]; + } + } + + return dp[n - 1]; + } +} From 5ead282cecf8457bd25abf24081f0fad2b393f26 Mon Sep 17 00:00:00 2001 From: HoonDongKang Date: Fri, 16 May 2025 13:55:01 +0900 Subject: [PATCH 5/5] add Set Matrix Zeroes solution --- set-matrix-zeroes/HoonDongKang.ts | 130 ++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 set-matrix-zeroes/HoonDongKang.ts diff --git a/set-matrix-zeroes/HoonDongKang.ts b/set-matrix-zeroes/HoonDongKang.ts new file mode 100644 index 000000000..f24f91387 --- /dev/null +++ b/set-matrix-zeroes/HoonDongKang.ts @@ -0,0 +1,130 @@ +/** + * [Problem]: [73] Set Matrix Zeroes + * (https://leetcode.com/problems/set-matrix-zeroes/description/) + */ +/** + Do not return anything, modify matrix in-place instead. + 1. 0의 모든 행과 열을 0으로 변환 + 2. 변경된 0과 기존 0의 차이를 만들어야 할 것 ( #으로 바꾸고 나중에 0 변환?) + */ + +function setZeroes(matrix: number[][] | string[][]): void { + //시간복잡도 O(n^2) + //공간복잡도 O(n) + //통과는 하였지만 타입 조건을 변경해서 풀이 + function bruteForceFunc(matrix: number[][] | string[][]): void { + const CHANGED_NUMBER = "#"; + const rows = matrix.length; + const cols = matrix[0].length; + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (matrix[i][j] === 0) { + let top = i - 1; + let bottom = i + 1; + let left = j - 1; + let right = j + 1; + + while (top >= 0) { + if (matrix[top][j] !== 0) matrix[top][j] = CHANGED_NUMBER; + top--; + } + while (bottom < rows) { + if (matrix[bottom][j] !== 0) matrix[bottom][j] = CHANGED_NUMBER; + bottom++; + } + while (left >= 0) { + if (matrix[i][left] !== 0) matrix[i][left] = CHANGED_NUMBER; + left--; + } + while (right < cols) { + if (matrix[i][right] !== 0) matrix[i][right] = CHANGED_NUMBER; + right++; + } + } + } + } + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (matrix[i][j] === CHANGED_NUMBER) matrix[i][j] = 0; + } + } + } + + //시간복잡도 O(m * n) + //공간복잡도 O(m + n) + function setFunc(matrix: number[][]): void { + const rows = matrix.length; + const cols = matrix[0].length; + let zeroRows = new Set(); + let zeroCols = new Set(); + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (matrix[i][j] === 0) { + zeroCols.add(j); + zeroRows.add(i); + } + } + } + + for (let r of zeroRows) { + for (let i = 0; i < cols; i++) { + matrix[r][i] = 0; + } + } + + for (let c of zeroCols) { + for (let i = 0; i < rows; i++) { + matrix[i][c] = 0; + } + } + } + + //시간복잡도 O(m * n) + //공간복잡도 O(1) + function optimizeSpaceFunc(matrix: number[][]): void { + const rows = matrix.length; + const cols = matrix[0].length; + let isFirstRowZero = false; + let isFirstColZero = false; + + for (let i = 0; i < rows; i++) { + if (matrix[i][0] === 0) isFirstColZero = true; + } + + for (let j = 0; j < cols; j++) { + if (matrix[0][j] === 0) isFirstRowZero = true; + } + + for (let i = 1; i < rows; i++) { + for (let j = 1; j < cols; j++) { + if (matrix[i][j] === 0) { + matrix[i][0] = 0; + matrix[0][j] = 0; + } + } + } + + for (let i = 1; i < rows; i++) { + for (let j = 1; j < cols; j++) { + if (matrix[i][0] === 0 || matrix[0][j] === 0) { + matrix[i][j] = 0; + } + } + } + + if (isFirstColZero) { + for (let i = 0; i < rows; i++) { + matrix[i][0] = 0; + } + } + + if (isFirstRowZero) { + for (let j = 0; j < cols; j++) { + matrix[0][j] = 0; + } + } + } +}