diff --git a/linked-list-cycle/HoonDongKang.ts b/linked-list-cycle/HoonDongKang.ts new file mode 100644 index 000000000..c827fe256 --- /dev/null +++ b/linked-list-cycle/HoonDongKang.ts @@ -0,0 +1,44 @@ +/** + * [Problem]: [141] Linked List Cycle + * (https://leetcode.com/problems/linked-list-cycle/description/) + */ +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 hasCycle(head: ListNode | null): boolean { + //시간복잡도 O(n) + //공간복잡도 O(n) + function setFunc(head: ListNode | null): boolean { + if (!head) return false; + const set = new Set(); + let currentNode: ListNode = head; + + while (currentNode) { + if (set.has(currentNode)) return true; + + set.add(currentNode); + currentNode = currentNode.next!; + } + + return false; + } + //시간복잡도 O(n) + //공간복잡도 O(1) + function pointerFunc(head: ListNode | null): boolean { + let slow = head; + let fast = head; + while (fast && fast.next) { + slow = slow.next; + fast = fast.next.next; + + if (slow === fast) return true; + } + return false; + } +} diff --git a/maximum-product-subarray/HoonDongKang.ts b/maximum-product-subarray/HoonDongKang.ts new file mode 100644 index 000000000..16ebad1a2 --- /dev/null +++ b/maximum-product-subarray/HoonDongKang.ts @@ -0,0 +1,43 @@ +/** + * [Problem]: [152] Maximum Product Subarray + * (https://leetcode.com/problems/maximum-product-subarray/description/) + */ +function maxProduct(nums: number[]): number { + // 시간복잡도 O(n^2) + // 공간복잡도 O(1) + function bruteForce(nums: number[]): number { + let maxProduct = nums[0]; + + for (let i = 0; i < nums.length; i++) { + let product = 1; + for (let j = i; j < nums.length; j++) { + product *= nums[j]; + maxProduct = Math.max(maxProduct, product); + } + } + + return maxProduct; + } + + // 시간복잡도 O(n) + // 공간복잡도 O(1) + function optimizedFunc(nums: number[]): number { + let maxResult = nums[0]; + let minResult = nums[0]; + let result = nums[0]; + + for (let i = 1; i < nums.length; i++) { + const num = nums[i]; + + if (num < 0) { + [maxResult, minResult] = [minResult, maxResult]; + } + + maxResult = Math.max(num, num * maxResult); + minResult = Math.min(num, num * minResult); + result = Math.max(result, maxResult); + } + + return result; + } +} diff --git a/minimum-window-substring/HoonDongKang.ts b/minimum-window-substring/HoonDongKang.ts new file mode 100644 index 000000000..0a63eab01 --- /dev/null +++ b/minimum-window-substring/HoonDongKang.ts @@ -0,0 +1,88 @@ +/** + * [Problem]: [76] Minimum Window Substring + * (https://leetcode.com/problems/minimum-window-substring/description/) + */ +function minWindow(s: string, t: string): string { + //시간복잡도 O(n) + //공간복잡도 O(n) + function windowFunc(s: string, t: string): string { + let left = 0; + let right = 0; + let s_map = new Map(); + let t_map = new Map(); + let minCount = Infinity; + let minStart = 0; + + for (const char of t) { + t_map.set(char, (t_map.get(char) || 0) + 1); + } + + while (right < s.length) { + let char = s[right]; + s_map.set(char, (s_map.get(char) || 0) + 1); + right++; + + while (isValid(s_map)) { + if (right - left < minCount) { + minCount = right - left; + minStart = left; + } + + let char = s[left]; + s_map.set(char, s_map.get(char)! - 1); + left++; + } + } + + return minCount === Infinity ? "" : s.slice(minStart, minStart + minCount); + + function isValid(map: Map): boolean { + for (let i of t_map.keys()) { + if ((map.get(i) || 0) < t_map.get(i)!) return false; + } + return true; + } + } + + //시간복잡도 O(n) + //공간복잡도 O(n) + function optimizedFunc(s: string, t: string): string { + const map = new Map(); + let required = t.length; + let left = 0; + let minLen = Infinity; + let minStart = 0; + + for (const char of t) { + map.set(char, (map.get(char) || 0) + 1); + } + + for (let right = 0; right < s.length; right++) { + const char = s[right]; + + if (map.has(char)) { + const count = map.get(char)!; + if (0 < count) required--; + map.set(char, count - 1); + } + + while (required === 0) { + const char = s[left]; + + if (right - left + 1 < minLen) { + minLen = right - left + 1; + minStart = left; + } + + if (map.has(char)) { + map.set(char, map.get(char)! + 1); + if (map.get(char)! > 0) { + required++; + } + } + left++; + } + } + return minLen === Infinity ? "" : s.slice(minStart, minStart + minLen); + } +} diff --git a/pacific-atlantic-water-flow/HoonDongKang.ts b/pacific-atlantic-water-flow/HoonDongKang.ts new file mode 100644 index 000000000..6924416e5 --- /dev/null +++ b/pacific-atlantic-water-flow/HoonDongKang.ts @@ -0,0 +1,53 @@ +/** + * [Problem]: [417] Pacific Atlantic Water Flow + * (https://leetcode.com/problems/pacific-atlantic-water-flow/description/) + */ +function pacificAtlantic(heights: number[][]): number[][] { + //시간복잡도 O(n) + //공간복잡도 O(n) + const result: number[][] = []; + const rows = heights.length; + const cols = heights[0].length; + + const pacificArea: boolean[][] = Array.from({ length: rows }, () => Array(cols).fill(false)); + const atlanticArea: boolean[][] = Array.from({ length: rows }, () => Array(cols).fill(false)); + + function dfs(visited: boolean[][], row: number, col: number) { + if (row < 0 || col < 0 || row >= rows || col >= cols) return; + if (visited[row][col]) return; + + visited[row][col] = true; + + [ + [row - 1, col], + [row + 1, col], + [row, col - 1], + [row, col + 1], + ].forEach(([r, c]) => { + if (r >= 0 && c >= 0 && r < rows && c < cols) { + if (heights[r][c] >= heights[row][col]) { + dfs(visited, r, c); + } + } + }); + } + + for (let i = 0; i < rows; i++) { + dfs(pacificArea, i, 0); + dfs(atlanticArea, i, cols - 1); + } + for (let j = 0; j < cols; j++) { + dfs(pacificArea, 0, j); + dfs(atlanticArea, rows - 1, j); + } + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (pacificArea[i][j] && atlanticArea[i][j]) { + result.push([i, j]); + } + } + } + + return result; +} diff --git a/sum-of-two-integers/HoonDongKang.ts b/sum-of-two-integers/HoonDongKang.ts new file mode 100644 index 000000000..6dc0a6830 --- /dev/null +++ b/sum-of-two-integers/HoonDongKang.ts @@ -0,0 +1,14 @@ +/** + * [Problem]: [371] Sum of Two Integers + * (https://leetcode.com/problems/sum-of-two-integers/description/) + */ +function getSum(a: number, b: number): number { + // 시간복잡도 O(n) + // 공간복잡도 O(1) + while (b !== 0) { + const carry = a & b; + a = a ^ b; + b = carry << 1; + } + return a; +}