Skip to content

Commit fcb4ee5

Browse files
authored
Merge pull request #370 from tolluset/main
[μ΄λ³‘ν˜„] Week 2
2 parents 8605a7d + 91f1443 commit fcb4ee5

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class TreeNode {
2+
val: number;
3+
left: TreeNode | null;
4+
right: TreeNode | null;
5+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
this.val = val === undefined ? 0 : val;
7+
this.left = left === undefined ? null : left;
8+
this.right = right === undefined ? null : right;
9+
}
10+
}
11+
12+
/**
13+
* TC: O(n^2)
14+
* SC: O(n)
15+
* */
16+
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
17+
if (!preorder?.length || !inorder?.length) {
18+
return null;
19+
}
20+
21+
const rootValue = preorder[0];
22+
const root = new TreeNode(rootValue);
23+
const inorderRootIndex = inorder.indexOf(rootValue);
24+
25+
root.left = buildTree(
26+
preorder.slice(1, inorderRootIndex + 1),
27+
inorder.slice(0, inorderRootIndex),
28+
);
29+
30+
root.right = buildTree(
31+
preorder.slice(inorderRootIndex + 1),
32+
inorder.slice(inorderRootIndex + 1),
33+
);
34+
35+
return root;
36+
}

β€Žcounting-bits/tolluset.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* TC: O(nlogn)
3+
* SC: O(nlogn)
4+
* */
5+
function countBits(n: number): number[] {
6+
return Array.from({ length: n + 1 }, (_, i) => i).map(
7+
(v) =>
8+
v
9+
.toString(2)
10+
.split("")
11+
.filter((v) => v === "1").length,
12+
);
13+
}

β€Ždecode-ways/tolluset.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* TC: O(n)
3+
* SC: O(n)
4+
*/
5+
function numDecodings(s: string): number {
6+
const n = s.length;
7+
const dp = new Array(n + 1).fill(0);
8+
9+
dp[0] = 1;
10+
dp[1] = Number(s[0]) === 0 ? 0 : 1;
11+
12+
for (let i = 2; i <= n; i++) {
13+
const oneDigit = Number(s.slice(i - 1, i));
14+
const twoDigits = Number(s.slice(i - 2, i));
15+
16+
if (oneDigit >= 1 && oneDigit <= 9) {
17+
dp[i] += dp[i - 1];
18+
}
19+
20+
if (twoDigits >= 10 && twoDigits <= 26) {
21+
dp[i] += dp[i - 2];
22+
}
23+
}
24+
25+
return dp[n];
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* TC: O(nm)
3+
* SC: O(nm)
4+
* */
5+
function encode(arr: string[]): string {
6+
return arr.join("πŸŽƒ");
7+
}
8+
9+
function decode(str: string): string[] {
10+
return str.split("πŸŽƒ");
11+
}

β€Žvalid-anagram/tolluset.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(n)
4+
* */
5+
function isAnagram(s: string, t: string): boolean {
6+
if (s.length !== t.length) {
7+
return false;
8+
}
9+
10+
const groupS = groupBy(s);
11+
const groupT = groupBy(t);
12+
13+
return Object.keys(groupS).every((k) => groupS[k] === groupT[k]);
14+
}
15+
16+
const groupBy = (v: string) =>
17+
v.split("").reduce((acc, cur) => ((acc[cur] = (acc[cur] ?? 0) + 1), acc), {});

0 commit comments

Comments
Β (0)