-
Notifications
You must be signed in to change notification settings - Fork 126
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f56c9a8
init: week 4
HC-kang f8436ce
Merge branch 'main' of https://github.com/HC-kang/leetcode-study
HC-kang 2d6485c
Feat: 125. Valid Palindrome
HC-kang fb85996
Feat: 268. Missing Number
HC-kang 6f1cdad
Docs: 125. Valid Palindrome
HC-kang f0b55b2
Feat: 128. Longest Consecutive Sequence
HC-kang 1b32cdc
Feat: 79. Word Search
HC-kang eeac423
Feat: 152. Maximum Product Subarray
HC-kang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요 따봉의 의미가 뭔지 궁금하네요 ㅋㅋ