From 97911b7f5fe32335d08a9b2ab34cdc2999708c92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sun, 1 Sep 2024 12:41:10 +0900 Subject: [PATCH 1/5] Add week 4 solutions: valid-palindrome --- valid-palindrome/gitsunmin.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 valid-palindrome/gitsunmin.ts diff --git a/valid-palindrome/gitsunmin.ts b/valid-palindrome/gitsunmin.ts new file mode 100644 index 00000000..cf2881f4 --- /dev/null +++ b/valid-palindrome/gitsunmin.ts @@ -0,0 +1,14 @@ +/** + * https://leetcode.com/problems/valid-palindrome + * time complexity : O(n) + * space complexity : O(n) + */ + +const clean = (s: string): string => s.toLowerCase().replace(/[^a-z0-9]/g, ""); + +const reverse = (s: string): string => s.split("").reverse().join(""); + +function isPalindrome(s: string): boolean { + const cleaned = clean(s); + return cleaned === reverse(cleaned); +}; From 6ef9db5aff252f2d3cb8b382ff45b2ad2aff4de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sun, 1 Sep 2024 13:04:24 +0900 Subject: [PATCH 2/5] Add week 4 solutions: missing-number --- missing-number/gitsunmin.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 missing-number/gitsunmin.ts diff --git a/missing-number/gitsunmin.ts b/missing-number/gitsunmin.ts new file mode 100644 index 00000000..351566ac --- /dev/null +++ b/missing-number/gitsunmin.ts @@ -0,0 +1,12 @@ +/** + * https://leetcode.com/problems/missing-number + * time complexity : O(n) + * space complexity : O(n) + */ +function missingNumber(nums: number[]): number { + const set = new Set(nums); + + for (let i = 0; i < set.size + 1; i++) if (!set.has(i)) return i; + + return 0; +}; From ab5d9de1991a69fe42d5fdfb5041eb99b23dfa2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sun, 1 Sep 2024 14:27:12 +0900 Subject: [PATCH 3/5] Add week 4 solutions: longest-consecutive-sequence --- longest-consecutive-sequence/gitsunmin.ts | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 longest-consecutive-sequence/gitsunmin.ts diff --git a/longest-consecutive-sequence/gitsunmin.ts b/longest-consecutive-sequence/gitsunmin.ts new file mode 100644 index 00000000..e209f1e9 --- /dev/null +++ b/longest-consecutive-sequence/gitsunmin.ts @@ -0,0 +1,29 @@ +/** + * https://leetcode.com/problems/longest-consecutive-sequence + * time complexity : O(n) + * space complexity : O(n) + */ +const findStreak = (set: Set) => (num: number): number => { + if (!set.has(num - 1)) return takeWhile(num, currentNum => set.has(currentNum)).length; + return 0; +}; + +const takeWhile = (start: number, predicate: (value: number) => boolean): number[] => { + const result: number[] = []; + let currentNum = start; + while (predicate(currentNum)) { + result.push(currentNum); + currentNum += 1; + } + return result; +} + +const max = (maxStreak: number, currentStreak: number): number => Math.max(maxStreak, currentStreak); + +function longestConsecutive(nums: number[]): number { + const numSet = new Set(nums); + + return [...numSet] + .map(findStreak(numSet)) + .reduce(max, 0); +} From a5426434ee4b4303a96241e36e0284cece620937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sun, 1 Sep 2024 22:51:24 +0900 Subject: [PATCH 4/5] Add week 4 solutions: word-search --- word-search/gitsunmin.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 word-search/gitsunmin.ts diff --git a/word-search/gitsunmin.ts b/word-search/gitsunmin.ts new file mode 100644 index 00000000..d47cdc0f --- /dev/null +++ b/word-search/gitsunmin.ts @@ -0,0 +1,33 @@ +/** + * https://leetcode.com/problems/word-search + * time complexity : O(m * n * 4^L) + * space complexity : O(L) + */ +const dfs = (r: number, c: number, index: number, board: string[][], word: string, rows: number, cols: number): boolean => { + if (index === word.length) return true; + + if (r < 0 || r >= rows || c < 0 || c >= cols || board[r][c] !== word[index]) return false; + const temp = board[r][c]; + + board[r][c] = '🚪'; + const found = dfs(r + 1, c, index + 1, board, word, rows, cols) || + dfs(r - 1, c, index + 1, board, word, rows, cols) || + dfs(r, c + 1, index + 1, board, word, rows, cols) || + dfs(r, c - 1, index + 1, board, word, rows, cols); + + board[r][c] = temp; + + return found; +}; + +function exist(board: string[][], word: string): boolean { + const rows = board.length; + const cols = board[0].length; + + for (let r = 0; r < rows; r++) { + for (let c = 0; c < cols; c++) { + if (dfs(r, c, 0, board, word, rows, cols)) return true; + } + } + return false; +} From b5bce2525eb7fe6cea0affcdf60eeb582fefa8f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Tue, 3 Sep 2024 19:54:58 +0900 Subject: [PATCH 5/5] Add week 4 solutions: maximum-product-subarray --- maximum-product-subarray/gitsunmin.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 maximum-product-subarray/gitsunmin.ts diff --git a/maximum-product-subarray/gitsunmin.ts b/maximum-product-subarray/gitsunmin.ts new file mode 100644 index 00000000..519d258f --- /dev/null +++ b/maximum-product-subarray/gitsunmin.ts @@ -0,0 +1,21 @@ +/** + * https://leetcode.com/problems/maximum-product-subarray + * time complexity : O(n) + * space complexity : O(1) + */ +function maxProduct(nums: number[]): number { + let r = nums[0]; + let mx = 1, mn = 1; + + for (let i = 0; i < nums.length; i++) { + const tempMx = mx * nums[i]; + const tempMn = mn * nums[i]; + + mx = Math.max(tempMx, tempMn, nums[i]); + mn = Math.min(tempMx, tempMn, nums[i]); + + r = Math.max(r, mx); + } + + return r; +}