From f56c9a89e625f0d23633bd1c9bd2736880f9a777 Mon Sep 17 00:00:00 2001 From: HC-kang Date: Mon, 2 Sep 2024 08:55:59 +0900 Subject: [PATCH 1/7] init: week 4 --- longest-consecutive-sequence/HC-kang.ts | 3 +++ maximum-product-subarray/HC-kang.ts | 3 +++ missing-number/HC-kang.ts | 3 +++ valid-palindrome/HC-kang.ts | 3 +++ word-search/HC-kang.ts | 3 +++ 5 files changed, 15 insertions(+) create mode 100644 longest-consecutive-sequence/HC-kang.ts create mode 100644 maximum-product-subarray/HC-kang.ts create mode 100644 missing-number/HC-kang.ts create mode 100644 valid-palindrome/HC-kang.ts create mode 100644 word-search/HC-kang.ts diff --git a/longest-consecutive-sequence/HC-kang.ts b/longest-consecutive-sequence/HC-kang.ts new file mode 100644 index 000000000..4cdf63803 --- /dev/null +++ b/longest-consecutive-sequence/HC-kang.ts @@ -0,0 +1,3 @@ +function longestConsecutive(nums: number[]): number { + return 0; +} diff --git a/maximum-product-subarray/HC-kang.ts b/maximum-product-subarray/HC-kang.ts new file mode 100644 index 000000000..fc0a10cd5 --- /dev/null +++ b/maximum-product-subarray/HC-kang.ts @@ -0,0 +1,3 @@ +function maxProduct(nums: number[]): number { + return 0; +} diff --git a/missing-number/HC-kang.ts b/missing-number/HC-kang.ts new file mode 100644 index 000000000..09c4785f6 --- /dev/null +++ b/missing-number/HC-kang.ts @@ -0,0 +1,3 @@ +function missingNumber(nums: number[]): number { + return 0; +} diff --git a/valid-palindrome/HC-kang.ts b/valid-palindrome/HC-kang.ts new file mode 100644 index 000000000..a6a68a6ce --- /dev/null +++ b/valid-palindrome/HC-kang.ts @@ -0,0 +1,3 @@ +function isPalindrome(s: string): boolean { + return false; +} diff --git a/word-search/HC-kang.ts b/word-search/HC-kang.ts new file mode 100644 index 000000000..de90414e1 --- /dev/null +++ b/word-search/HC-kang.ts @@ -0,0 +1,3 @@ +function exist(board: string[][], word: string): boolean { + return false; +} From 2d6485cbf845f8bd6aadb97595b85fe57e6a872e Mon Sep 17 00:00:00 2001 From: HC-kang Date: Tue, 3 Sep 2024 08:19:15 +0900 Subject: [PATCH 2/7] Feat: 125. Valid Palindrome --- valid-palindrome/HC-kang.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/valid-palindrome/HC-kang.ts b/valid-palindrome/HC-kang.ts index a6a68a6ce..adb2040e5 100644 --- a/valid-palindrome/HC-kang.ts +++ b/valid-palindrome/HC-kang.ts @@ -1,3 +1,23 @@ +/** + * https://leetcode.com/problems/valid-palindrome/ + */ function isPalindrome(s: string): boolean { - return false; + function isAlNum(char: string): boolean { + return /^[a-zA-Z0-9]$/.test(char); + } + + let left = 0; + let right = s.length - 1; + while (left < right) { + while (left < right && !isAlNum(s[left])) left++; + while (left < right && !isAlNum(s[right])) right--; + + if (s[left].toLowerCase() !== s[right].toLowerCase()) { + return false; + } + + left++; + right--; + } + return true; } From fb859968dc570b7198a8ea47422a4f63ac420c94 Mon Sep 17 00:00:00 2001 From: HC-kang Date: Tue, 3 Sep 2024 08:30:48 +0900 Subject: [PATCH 3/7] Feat: 268. Missing Number --- missing-number/HC-kang.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/missing-number/HC-kang.ts b/missing-number/HC-kang.ts index 09c4785f6..d8e156b9a 100644 --- a/missing-number/HC-kang.ts +++ b/missing-number/HC-kang.ts @@ -1,3 +1,10 @@ +/** + * https://leetcode.com/problems/missing-number/ + * T.C.: O(n) + * S.C.: O(1) + */ function missingNumber(nums: number[]): number { - return 0; + let sum = nums.length; // i for 0 to n-1. So, n is missing. + for (let i = 0; i < nums.length; i++) sum = sum + i - nums[i]; + return sum; } From 6f1cdadbf34aedcdecf8b66f385be87029f494f0 Mon Sep 17 00:00:00 2001 From: HC-kang Date: Tue, 3 Sep 2024 08:31:04 +0900 Subject: [PATCH 4/7] Docs: 125. Valid Palindrome --- valid-palindrome/HC-kang.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/valid-palindrome/HC-kang.ts b/valid-palindrome/HC-kang.ts index adb2040e5..b6a6d28af 100644 --- a/valid-palindrome/HC-kang.ts +++ b/valid-palindrome/HC-kang.ts @@ -1,5 +1,7 @@ /** * https://leetcode.com/problems/valid-palindrome/ + * T.C.: O(n) + * S.C.: O(1) */ function isPalindrome(s: string): boolean { function isAlNum(char: string): boolean { From f0b55b291c6dba67f737ee9372bd1dae745ac2f1 Mon Sep 17 00:00:00 2001 From: HC-kang Date: Tue, 3 Sep 2024 09:02:14 +0900 Subject: [PATCH 5/7] Feat: 128. Longest Consecutive Sequence --- longest-consecutive-sequence/HC-kang.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/longest-consecutive-sequence/HC-kang.ts b/longest-consecutive-sequence/HC-kang.ts index 4cdf63803..9d048bfe7 100644 --- a/longest-consecutive-sequence/HC-kang.ts +++ b/longest-consecutive-sequence/HC-kang.ts @@ -1,3 +1,24 @@ +/** + * https://leetcode.com/problems/longest-consecutive-sequence/ + * T.C.: O(n) + * S.C.: O(n) + */ function longestConsecutive(nums: number[]): number { - return 0; + const numSet = new Set(nums); + let max = 0; + + for (const num of numSet) { + if (numSet.has(num - 1)) { + continue; + } + + let count = 0; + while (numSet.has(num + count)) { + count++; + } + + if (count > max) max = count; + } + + return max; } From 1b32cdc6e7a84011179a7c46fd1e85720c6d585f Mon Sep 17 00:00:00 2001 From: HC-kang Date: Wed, 4 Sep 2024 20:28:16 +0900 Subject: [PATCH 6/7] Feat: 79. Word Search --- word-search/HC-kang.ts | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/word-search/HC-kang.ts b/word-search/HC-kang.ts index de90414e1..22ffb48e7 100644 --- a/word-search/HC-kang.ts +++ b/word-search/HC-kang.ts @@ -1,3 +1,57 @@ +/** + * https://leetcode.com/problems/word-search + * T.C. O(n * m * 4^l) + * S.C. O(n * m) + */ function exist(board: string[][], word: string): boolean { + const countMap = new Map(); + + for (let i = 0; i < word.length; i++) { + countMap.set(word[i], (countMap.get(word[i]) || 0) + 1); + } + for (let i = 0; i < board.length * board[0].length; i++) { + countMap.set(board[Math.floor(i / board[0].length)][i % board[0].length], 0); + } + + if (Array.from(countMap.values()).some((v) => v > 0)) return false; + + let seen = Array.from({ length: board.length }, () => + new Array(board[0].length).fill(false) + ); + const directives = [ + [0, 1], + [0, -1], + [1, 0], + [-1, 0], + ]; + + function dfs(h: number, w: number, index: number): boolean { + if (w < 0 || w >= board[0].length) return false; + if (h < 0 || h >= board.length) return false; + + if (seen[h][w]) return false; + if (board[h][w] !== word[index]) return false; + if (index === word.length - 1) return true; + + seen[h][w] = true; + + for (let i = 0; i < 4; i++) { + const [dh, dw] = directives[i]; + if (dfs(h + dh, w + dw, index + 1)) { + return true; + } + } + + seen[h][w] = false; + return false; + } + + for (let i = 0; i < board.length; i++) { + for (let j = 0; j < board[0].length; j++) { + if (dfs(i, j, 0)) { + return true; + } + } + } return false; } From eeac4235cfa82da056f2e7e1cb958bc1f1c59b64 Mon Sep 17 00:00:00 2001 From: HC-kang Date: Wed, 4 Sep 2024 23:14:44 +0900 Subject: [PATCH 7/7] Feat: 152. Maximum Product Subarray --- maximum-product-subarray/HC-kang.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/maximum-product-subarray/HC-kang.ts b/maximum-product-subarray/HC-kang.ts index fc0a10cd5..312f744ec 100644 --- a/maximum-product-subarray/HC-kang.ts +++ b/maximum-product-subarray/HC-kang.ts @@ -1,3 +1,26 @@ +/** + * https://leetcode.com/problems/maximum-product-subarray + * T.C. O(n) + * S.C. O(1) + * All numbers are integers, so multiplying two numbers cannot result in a smaller absolute value. + * It's important to pay attention to zeros and negative numbers. + */ function maxProduct(nums: number[]): number { - return 0; + if (nums.length === 0) return 0; + + let max = nums[0]; + let min = nums[0]; + let result = nums[0]; + + for (let i = 1; i < nums.length; i++) { + const num = nums[i]; + if (num < 0) [max, min] = [min, max]; + + max = Math.max(num, max * num); + min = Math.min(num, min * num); + + result = Math.max(result, max); + } + + return result; }