-
Notifications
You must be signed in to change notification settings - Fork 126
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 #412 from tolluset/main
[μ΄λ³ν] Week 4
- Loading branch information
Showing
5 changed files
with
214 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,42 @@ | ||
/* | ||
* TC: O(n) | ||
* SC: O(n) | ||
* */ | ||
function longestConsecutive(nums: number[]): number { | ||
const n = nums.length; | ||
|
||
if (n <= 1) { | ||
return n; | ||
} | ||
|
||
const numsSet = new Set(nums); | ||
let longestLen = 0; | ||
|
||
for (let i = 0; i < n; i++) { | ||
let current = nums[i]; | ||
|
||
if (!numsSet.has(current - 1)) { | ||
let currentLen = 1; | ||
|
||
while (numsSet.has(current + 1)) { | ||
current++; | ||
currentLen++; | ||
} | ||
|
||
longestLen = Math.max(longestLen, currentLen); | ||
} | ||
} | ||
return longestLen; | ||
} | ||
|
||
const t1 = longestConsecutive([100, 4, 200, 1, 3, 2]); | ||
console.info("π : tolluset.ts:5: t1=", t1); // 4 | ||
|
||
const t2 = longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]); | ||
console.info("π : tolluset.ts:8: t2=", t2); // 9 | ||
|
||
const t3 = longestConsecutive([9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6]); | ||
console.info("π : tolluset.ts:40: t3=", t3); // 7 | ||
|
||
const t4 = longestConsecutive([-6, -1, -1, 9, -8, -6, -6, 4, 4, -3, -8, -1]); | ||
console.info("π : tolluset.ts:59: t4=", t4); // 1 |
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,39 @@ | ||
/* | ||
* TC: O(n) | ||
* SC: O(1) | ||
* */ | ||
function maxProduct(nums: number[]): number { | ||
const n = nums.length; | ||
|
||
if (n === 1) { | ||
return nums[0]; | ||
} | ||
|
||
let max = 0, | ||
min = 0, | ||
res = 0; | ||
|
||
for (let i = 0; i < n; i++) { | ||
const cur = nums[i]; | ||
|
||
if (cur < 0) { | ||
[max, min] = [min, max]; | ||
} | ||
|
||
max = Math.max(cur, max * cur); | ||
min = Math.min(cur, min * cur); | ||
|
||
res = Math.max(res, max); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
const t1 = maxProduct([2, 3, -2, 4]); | ||
console.info("π : tolluset.ts:3: t1=", t1); // 6 | ||
|
||
const t2 = maxProduct([-2, 0, -1]); | ||
console.info("π : tolluset.ts:6: t2=", t2); // 0 | ||
|
||
const t3 = maxProduct([-2]); | ||
console.info("π : tolluset.ts:34: t3=", t3); // -2 |
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 @@ | ||
/* | ||
* TC: O(n) | ||
* SC: O(1) | ||
* */ | ||
function missingNumber(nums: number[]): number { | ||
const n = nums.length; | ||
|
||
const sum = (n * (n + 1)) / 2; | ||
const actualSum = nums.reduce((acc, curr) => acc + curr, 0); | ||
|
||
return sum - actualSum; | ||
} | ||
|
||
const t1 = missingNumber([3, 0, 1]); | ||
console.info("π : tolluset.ts:3: t1=", t1); // 2 | ||
|
||
const t2 = missingNumber([0, 1]); | ||
console.info("π : tolluset.ts:6: t2=", t2); // 2 | ||
|
||
const t3 = missingNumber([9, 6, 4, 2, 3, 5, 7, 0, 1]); | ||
console.info("π : tolluset.ts:9: t3=", t3); // 8 |
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 @@ | ||
/* | ||
* TC: O(n) | ||
* SC: O(n) | ||
* */ | ||
function isPalindrome(s: string): boolean { | ||
const parsedString = s.replace(/[^A-Za-z0-9]/g, "").toLowerCase(); | ||
const n = parsedString.length; | ||
|
||
if (n === 0) { | ||
return true; | ||
} | ||
|
||
for (let i = 0; i < n / 2; i++) { | ||
if (parsedString[i] !== parsedString[n - i - 1]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
const t1 = isPalindrome("A man, a plan, a canal: Panama"); | ||
console.info("π : tolluset.ts:18: t1=", t1); // true | ||
|
||
const t2 = isPalindrome("race a car"); | ||
console.info("π : tolluset.ts:21: t2=", t2); // false | ||
|
||
const t3 = isPalindrome(" "); | ||
console.info("π : tolluset.ts:24: t3=", t3); // ture |
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,83 @@ | ||
/* | ||
* TC: O(row * column * 4^words.length) | ||
* SC: O(n) | ||
* */ | ||
function exist(board: string[][], word: string): boolean { | ||
const words = word.split(""); | ||
const n = words.length; | ||
|
||
const rowLen = board.length; | ||
const columnLen = board[0].length; | ||
|
||
const dfs = (row: number, column: number, cursor: number) => { | ||
if (cursor === n) { | ||
return true; | ||
} | ||
|
||
if ( | ||
row < 0 || | ||
row >= rowLen || | ||
column < 0 || | ||
column >= columnLen || | ||
board[row][column] !== words[cursor] | ||
) { | ||
return false; | ||
} | ||
|
||
const current = board[row][column]; | ||
board[row][column] = ""; | ||
|
||
if ( | ||
dfs(row - 1, column, cursor + 1) || | ||
dfs(row + 1, column, cursor + 1) || | ||
dfs(row, column - 1, cursor + 1) || | ||
dfs(row, column + 1, cursor + 1) | ||
) { | ||
return true; | ||
} | ||
|
||
board[row][column] = current; | ||
|
||
return false; | ||
}; | ||
|
||
for (let row = 0; row < rowLen; row++) { | ||
for (let column = 0; column < columnLen; column++) { | ||
if (dfs(row, column, 0)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
const t1 = exist( | ||
[ | ||
["A", "B", "C", "E"], | ||
["S", "F", "C", "S"], | ||
["A", "D", "E", "E"], | ||
], | ||
"ABCCED", | ||
); | ||
console.info("π : tolluset.ts:5: t1=", t1); // true | ||
|
||
const t2 = exist( | ||
[ | ||
["A", "B", "C", "E"], | ||
["S", "F", "C", "S"], | ||
["A", "D", "E", "E"], | ||
], | ||
"SEE", | ||
); | ||
console.info("π : tolluset.ts:15: t2=", t2); // true | ||
|
||
const t3 = exist( | ||
[ | ||
["A", "B", "C", "E"], | ||
["S", "F", "C", "S"], | ||
["A", "D", "E", "E"], | ||
], | ||
"ABCB", | ||
); | ||
console.info("π : tolluset.ts:24: t3=", t3); // false |