Skip to content

Commit 0f69d7f

Browse files
authored
Merge pull request #416 from HC-kang/main
[강희찬] WEEK 4 Solution
2 parents 05c0932 + eeac423 commit 0f69d7f

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* https://leetcode.com/problems/longest-consecutive-sequence/
3+
* T.C.: O(n)
4+
* S.C.: O(n)
5+
*/
6+
function longestConsecutive(nums: number[]): number {
7+
const numSet = new Set(nums);
8+
let max = 0;
9+
10+
for (const num of numSet) {
11+
if (numSet.has(num - 1)) {
12+
continue;
13+
}
14+
15+
let count = 0;
16+
while (numSet.has(num + count)) {
17+
count++;
18+
}
19+
20+
if (count > max) max = count;
21+
}
22+
23+
return max;
24+
}

maximum-product-subarray/HC-kang.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* https://leetcode.com/problems/maximum-product-subarray
3+
* T.C. O(n)
4+
* S.C. O(1)
5+
* All numbers are integers, so multiplying two numbers cannot result in a smaller absolute value.
6+
* It's important to pay attention to zeros and negative numbers.
7+
*/
8+
function maxProduct(nums: number[]): number {
9+
if (nums.length === 0) return 0;
10+
11+
let max = nums[0];
12+
let min = nums[0];
13+
let result = nums[0];
14+
15+
for (let i = 1; i < nums.length; i++) {
16+
const num = nums[i];
17+
if (num < 0) [max, min] = [min, max];
18+
19+
max = Math.max(num, max * num);
20+
min = Math.min(num, min * num);
21+
22+
result = Math.max(result, max);
23+
}
24+
25+
return result;
26+
}

missing-number/HC-kang.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* https://leetcode.com/problems/missing-number/
3+
* T.C.: O(n)
4+
* S.C.: O(1)
5+
*/
6+
function missingNumber(nums: number[]): number {
7+
let sum = nums.length; // i for 0 to n-1. So, n is missing.
8+
for (let i = 0; i < nums.length; i++) sum = sum + i - nums[i];
9+
return sum;
10+
}

valid-palindrome/HC-kang.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* https://leetcode.com/problems/valid-palindrome/
3+
* T.C.: O(n)
4+
* S.C.: O(1)
5+
*/
6+
function isPalindrome(s: string): boolean {
7+
function isAlNum(char: string): boolean {
8+
return /^[a-zA-Z0-9]$/.test(char);
9+
}
10+
11+
let left = 0;
12+
let right = s.length - 1;
13+
while (left < right) {
14+
while (left < right && !isAlNum(s[left])) left++;
15+
while (left < right && !isAlNum(s[right])) right--;
16+
17+
if (s[left].toLowerCase() !== s[right].toLowerCase()) {
18+
return false;
19+
}
20+
21+
left++;
22+
right--;
23+
}
24+
return true;
25+
}

word-search/HC-kang.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* https://leetcode.com/problems/word-search
3+
* T.C. O(n * m * 4^l)
4+
* S.C. O(n * m)
5+
*/
6+
function exist(board: string[][], word: string): boolean {
7+
const countMap = new Map<string, number>();
8+
9+
for (let i = 0; i < word.length; i++) {
10+
countMap.set(word[i], (countMap.get(word[i]) || 0) + 1);
11+
}
12+
for (let i = 0; i < board.length * board[0].length; i++) {
13+
countMap.set(board[Math.floor(i / board[0].length)][i % board[0].length], 0);
14+
}
15+
16+
if (Array.from(countMap.values()).some((v) => v > 0)) return false;
17+
18+
let seen = Array.from({ length: board.length }, () =>
19+
new Array(board[0].length).fill(false)
20+
);
21+
const directives = [
22+
[0, 1],
23+
[0, -1],
24+
[1, 0],
25+
[-1, 0],
26+
];
27+
28+
function dfs(h: number, w: number, index: number): boolean {
29+
if (w < 0 || w >= board[0].length) return false;
30+
if (h < 0 || h >= board.length) return false;
31+
32+
if (seen[h][w]) return false;
33+
if (board[h][w] !== word[index]) return false;
34+
if (index === word.length - 1) return true;
35+
36+
seen[h][w] = true;
37+
38+
for (let i = 0; i < 4; i++) {
39+
const [dh, dw] = directives[i];
40+
if (dfs(h + dh, w + dw, index + 1)) {
41+
return true;
42+
}
43+
}
44+
45+
seen[h][w] = false;
46+
return false;
47+
}
48+
49+
for (let i = 0; i < board.length; i++) {
50+
for (let j = 0; j < board[0].length; j++) {
51+
if (dfs(i, j, 0)) {
52+
return true;
53+
}
54+
}
55+
}
56+
return false;
57+
}

0 commit comments

Comments
 (0)