Skip to content

Commit 7c3afab

Browse files
authored
Merge pull request #412 from tolluset/main
[μ΄λ³‘ν˜„] Week 4
2 parents d561e58 + 378093f commit 7c3afab

File tree

5 files changed

+214
-0
lines changed

5 files changed

+214
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(n)
4+
* */
5+
function longestConsecutive(nums: number[]): number {
6+
const n = nums.length;
7+
8+
if (n <= 1) {
9+
return n;
10+
}
11+
12+
const numsSet = new Set(nums);
13+
let longestLen = 0;
14+
15+
for (let i = 0; i < n; i++) {
16+
let current = nums[i];
17+
18+
if (!numsSet.has(current - 1)) {
19+
let currentLen = 1;
20+
21+
while (numsSet.has(current + 1)) {
22+
current++;
23+
currentLen++;
24+
}
25+
26+
longestLen = Math.max(longestLen, currentLen);
27+
}
28+
}
29+
return longestLen;
30+
}
31+
32+
const t1 = longestConsecutive([100, 4, 200, 1, 3, 2]);
33+
console.info("πŸš€ : tolluset.ts:5: t1=", t1); // 4
34+
35+
const t2 = longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]);
36+
console.info("πŸš€ : tolluset.ts:8: t2=", t2); // 9
37+
38+
const t3 = longestConsecutive([9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6]);
39+
console.info("πŸš€ : tolluset.ts:40: t3=", t3); // 7
40+
41+
const t4 = longestConsecutive([-6, -1, -1, 9, -8, -6, -6, 4, 4, -3, -8, -1]);
42+
console.info("πŸš€ : tolluset.ts:59: t4=", t4); // 1

β€Žmaximum-subarray/tolluset.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(1)
4+
* */
5+
function maxProduct(nums: number[]): number {
6+
const n = nums.length;
7+
8+
if (n === 1) {
9+
return nums[0];
10+
}
11+
12+
let max = 0,
13+
min = 0,
14+
res = 0;
15+
16+
for (let i = 0; i < n; i++) {
17+
const cur = nums[i];
18+
19+
if (cur < 0) {
20+
[max, min] = [min, max];
21+
}
22+
23+
max = Math.max(cur, max * cur);
24+
min = Math.min(cur, min * cur);
25+
26+
res = Math.max(res, max);
27+
}
28+
29+
return res;
30+
}
31+
32+
const t1 = maxProduct([2, 3, -2, 4]);
33+
console.info("πŸš€ : tolluset.ts:3: t1=", t1); // 6
34+
35+
const t2 = maxProduct([-2, 0, -1]);
36+
console.info("πŸš€ : tolluset.ts:6: t2=", t2); // 0
37+
38+
const t3 = maxProduct([-2]);
39+
console.info("πŸš€ : tolluset.ts:34: t3=", t3); // -2

β€Žmissing-number/tolluset.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(1)
4+
* */
5+
function missingNumber(nums: number[]): number {
6+
const n = nums.length;
7+
8+
const sum = (n * (n + 1)) / 2;
9+
const actualSum = nums.reduce((acc, curr) => acc + curr, 0);
10+
11+
return sum - actualSum;
12+
}
13+
14+
const t1 = missingNumber([3, 0, 1]);
15+
console.info("πŸš€ : tolluset.ts:3: t1=", t1); // 2
16+
17+
const t2 = missingNumber([0, 1]);
18+
console.info("πŸš€ : tolluset.ts:6: t2=", t2); // 2
19+
20+
const t3 = missingNumber([9, 6, 4, 2, 3, 5, 7, 0, 1]);
21+
console.info("πŸš€ : tolluset.ts:9: t3=", t3); // 8

β€Žvalid-palindrome/tolluset.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(n)
4+
* */
5+
function isPalindrome(s: string): boolean {
6+
const parsedString = s.replace(/[^A-Za-z0-9]/g, "").toLowerCase();
7+
const n = parsedString.length;
8+
9+
if (n === 0) {
10+
return true;
11+
}
12+
13+
for (let i = 0; i < n / 2; i++) {
14+
if (parsedString[i] !== parsedString[n - i - 1]) {
15+
return false;
16+
}
17+
}
18+
19+
return true;
20+
}
21+
22+
const t1 = isPalindrome("A man, a plan, a canal: Panama");
23+
console.info("πŸš€ : tolluset.ts:18: t1=", t1); // true
24+
25+
const t2 = isPalindrome("race a car");
26+
console.info("πŸš€ : tolluset.ts:21: t2=", t2); // false
27+
28+
const t3 = isPalindrome(" ");
29+
console.info("πŸš€ : tolluset.ts:24: t3=", t3); // ture

β€Žword-search/tolluset.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* TC: O(row * column * 4^words.length)
3+
* SC: O(n)
4+
* */
5+
function exist(board: string[][], word: string): boolean {
6+
const words = word.split("");
7+
const n = words.length;
8+
9+
const rowLen = board.length;
10+
const columnLen = board[0].length;
11+
12+
const dfs = (row: number, column: number, cursor: number) => {
13+
if (cursor === n) {
14+
return true;
15+
}
16+
17+
if (
18+
row < 0 ||
19+
row >= rowLen ||
20+
column < 0 ||
21+
column >= columnLen ||
22+
board[row][column] !== words[cursor]
23+
) {
24+
return false;
25+
}
26+
27+
const current = board[row][column];
28+
board[row][column] = "";
29+
30+
if (
31+
dfs(row - 1, column, cursor + 1) ||
32+
dfs(row + 1, column, cursor + 1) ||
33+
dfs(row, column - 1, cursor + 1) ||
34+
dfs(row, column + 1, cursor + 1)
35+
) {
36+
return true;
37+
}
38+
39+
board[row][column] = current;
40+
41+
return false;
42+
};
43+
44+
for (let row = 0; row < rowLen; row++) {
45+
for (let column = 0; column < columnLen; column++) {
46+
if (dfs(row, column, 0)) {
47+
return true;
48+
}
49+
}
50+
}
51+
52+
return false;
53+
}
54+
55+
const t1 = exist(
56+
[
57+
["A", "B", "C", "E"],
58+
["S", "F", "C", "S"],
59+
["A", "D", "E", "E"],
60+
],
61+
"ABCCED",
62+
);
63+
console.info("πŸš€ : tolluset.ts:5: t1=", t1); // true
64+
65+
const t2 = exist(
66+
[
67+
["A", "B", "C", "E"],
68+
["S", "F", "C", "S"],
69+
["A", "D", "E", "E"],
70+
],
71+
"SEE",
72+
);
73+
console.info("πŸš€ : tolluset.ts:15: t2=", t2); // true
74+
75+
const t3 = exist(
76+
[
77+
["A", "B", "C", "E"],
78+
["S", "F", "C", "S"],
79+
["A", "D", "E", "E"],
80+
],
81+
"ABCB",
82+
);
83+
console.info("πŸš€ : tolluset.ts:24: t3=", t3); // false

0 commit comments

Comments
Β (0)