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 6 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;
}
3 changes: 3 additions & 0 deletions maximum-product-subarray/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function maxProduct(nums: number[]): number {
return 0;
}
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;
}
3 changes: 3 additions & 0 deletions word-search/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function exist(board: string[][], word: string): boolean {
return false;
}