-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #420 from gitsunmin/main
[gitsunmin] WEEK4 Solution
- Loading branch information
Showing
5 changed files
with
109 additions
and
0 deletions.
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,29 @@ | ||
/** | ||
* https://leetcode.com/problems/longest-consecutive-sequence | ||
* time complexity : O(n) | ||
* space complexity : O(n) | ||
*/ | ||
const findStreak = (set: Set<number>) => (num: number): number => { | ||
if (!set.has(num - 1)) return takeWhile(num, currentNum => set.has(currentNum)).length; | ||
return 0; | ||
}; | ||
|
||
const takeWhile = (start: number, predicate: (value: number) => boolean): number[] => { | ||
const result: number[] = []; | ||
let currentNum = start; | ||
while (predicate(currentNum)) { | ||
result.push(currentNum); | ||
currentNum += 1; | ||
} | ||
return result; | ||
} | ||
|
||
const max = (maxStreak: number, currentStreak: number): number => Math.max(maxStreak, currentStreak); | ||
|
||
function longestConsecutive(nums: number[]): number { | ||
const numSet = new Set(nums); | ||
|
||
return [...numSet] | ||
.map(findStreak(numSet)) | ||
.reduce(max, 0); | ||
} |
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,21 @@ | ||
/** | ||
* https://leetcode.com/problems/maximum-product-subarray | ||
* time complexity : O(n) | ||
* space complexity : O(1) | ||
*/ | ||
function maxProduct(nums: number[]): number { | ||
let r = nums[0]; | ||
let mx = 1, mn = 1; | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
const tempMx = mx * nums[i]; | ||
const tempMn = mn * nums[i]; | ||
|
||
mx = Math.max(tempMx, tempMn, nums[i]); | ||
mn = Math.min(tempMx, tempMn, nums[i]); | ||
|
||
r = Math.max(r, mx); | ||
} | ||
|
||
return r; | ||
} |
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,12 @@ | ||
/** | ||
* https://leetcode.com/problems/missing-number | ||
* time complexity : O(n) | ||
* space complexity : O(n) | ||
*/ | ||
function missingNumber(nums: number[]): number { | ||
const set = new Set<number>(nums); | ||
|
||
for (let i = 0; i < set.size + 1; i++) if (!set.has(i)) return i; | ||
|
||
return 0; | ||
}; |
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,14 @@ | ||
/** | ||
* https://leetcode.com/problems/valid-palindrome | ||
* time complexity : O(n) | ||
* space complexity : O(n) | ||
*/ | ||
|
||
const clean = (s: string): string => s.toLowerCase().replace(/[^a-z0-9]/g, ""); | ||
|
||
const reverse = (s: string): string => s.split("").reverse().join(""); | ||
|
||
function isPalindrome(s: string): boolean { | ||
const cleaned = clean(s); | ||
return cleaned === reverse(cleaned); | ||
}; |
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,33 @@ | ||
/** | ||
* https://leetcode.com/problems/word-search | ||
* time complexity : O(m * n * 4^L) | ||
* space complexity : O(L) | ||
*/ | ||
const dfs = (r: number, c: number, index: number, board: string[][], word: string, rows: number, cols: number): boolean => { | ||
if (index === word.length) return true; | ||
|
||
if (r < 0 || r >= rows || c < 0 || c >= cols || board[r][c] !== word[index]) return false; | ||
const temp = board[r][c]; | ||
|
||
board[r][c] = '🚪'; | ||
const found = dfs(r + 1, c, index + 1, board, word, rows, cols) || | ||
dfs(r - 1, c, index + 1, board, word, rows, cols) || | ||
dfs(r, c + 1, index + 1, board, word, rows, cols) || | ||
dfs(r, c - 1, index + 1, board, word, rows, cols); | ||
|
||
board[r][c] = temp; | ||
|
||
return found; | ||
}; | ||
|
||
function exist(board: string[][], word: string): boolean { | ||
const rows = board.length; | ||
const cols = board[0].length; | ||
|
||
for (let r = 0; r < rows; r++) { | ||
for (let c = 0; c < cols; c++) { | ||
if (dfs(r, c, 0, board, word, rows, cols)) return true; | ||
} | ||
} | ||
return false; | ||
} |