Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[강희찬] WEEK 4 Solution #416

Merged
merged 8 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions longest-consecutive-sequence/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* https://leetcode.com/problems/longest-consecutive-sequence/
* T.C.: O(n)
* S.C.: O(n)
*/
function longestConsecutive(nums: number[]): number {
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;
}
26 changes: 26 additions & 0 deletions maximum-product-subarray/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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 {
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요 따봉의 의미가 뭔지 궁금하네요 ㅋㅋ

}

return result;
}
10 changes: 10 additions & 0 deletions missing-number/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* https://leetcode.com/problems/missing-number/
* T.C.: O(n)
* S.C.: O(1)
*/
function missingNumber(nums: number[]): number {
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;
DaleSeo marked this conversation as resolved.
Show resolved Hide resolved
}
25 changes: 25 additions & 0 deletions valid-palindrome/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* https://leetcode.com/problems/valid-palindrome/
* T.C.: O(n)
* S.C.: O(1)
*/
function isPalindrome(s: string): boolean {
function isAlNum(char: string): boolean {
return /^[a-zA-Z0-9]$/.test(char);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다른 자바스크립트 답안들은 대소 비교를 많이 했던데 정규식이 훨씬 깔끔하네요!

}

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;
}
57 changes: 57 additions & 0 deletions word-search/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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<string, number>();

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;
}