Skip to content

Commit 2397584

Browse files
authored
Merge pull request DaleStudy#356 from HC-kang/main
[강희찬] WEEK 2 Solution
2 parents c9df583 + d4e9f48 commit 2397584

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Definition for a binary tree node.
2+
class TreeNode {
3+
val: number;
4+
left: TreeNode | null;
5+
right: TreeNode | null;
6+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
7+
this.val = val === undefined ? 0 : val;
8+
this.left = left === undefined ? null : left;
9+
this.right = right === undefined ? null : right;
10+
}
11+
}
12+
13+
// T.C: O(N)
14+
// S.C: O(N^2) - Slice makes n-1, n-2, ..., 1 for n times. So, it's O(N^2).
15+
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
16+
if (preorder.length === 0 || inorder.length === 0) {
17+
return null;
18+
}
19+
const root = new TreeNode(preorder[0]);
20+
const idx = inorder.indexOf(preorder[0]);
21+
root.left = buildTree(preorder.slice(1, idx + 1), inorder.slice(0, idx));
22+
root.right = buildTree(preorder.slice(idx + 1), inorder.slice(idx + 1));
23+
24+
return root;
25+
}
26+
27+
// Not using slice. but I think it's not necessary... first solution is more readable. and that's not so bad.
28+
// T.C: O(N)
29+
// S.C: O(N)
30+
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
31+
// this tree is consist of unique values
32+
const inorderMap = new Map<number, number>();
33+
for (const [i, val] of inorder.entries()) {
34+
inorderMap.set(val, i);
35+
}
36+
37+
function helper(preLeft: number, preRight: number, inLeft: number, inRight: number): TreeNode | null {
38+
if (preLeft > preRight) return null;
39+
40+
const rootValue = preorder[preLeft];
41+
const root = new TreeNode(rootValue);
42+
const inRootIdx = inorderMap.get(rootValue)!;
43+
44+
const leftSize = inRootIdx - inLeft;
45+
46+
root.left = helper(preLeft + 1, preLeft + leftSize, inLeft, inRootIdx - 1);
47+
root.right = helper(preLeft + leftSize + 1, preRight, inRootIdx + 1, inRight);
48+
49+
return root;
50+
}
51+
52+
return helper(0, preorder.length - 1, 0, inorder.length - 1);
53+
}

counting-bits/HC-kang.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// T.C: O(n)
2+
// S.C: O(n)
3+
function countBits(n: number): number[] {
4+
// T.C: O(1)
5+
// S.C: O(1)
6+
function countBit(num: number): number {
7+
num = num - ((num >>> 1) & 0x55555555);
8+
num = (num & 0x33333333) + ((num >>> 2) & 0x33333333);
9+
num = (num + (num >>> 4)) & 0x0f0f0f0f;
10+
num = num + (num >>> 8);
11+
num = num + (num >>> 16);
12+
return num & 0x3f;
13+
}
14+
15+
return new Array(n + 1).fill(0).map((_, i) => countBit(i));
16+
}
17+
18+
// T.C: O(n)
19+
// S.C: O(n)
20+
function countBits(n: number): number[] {
21+
const dp = new Array(n + 1).fill(0);
22+
for (let i = 1; i <= n; i++) {
23+
dp[i] = dp[i >> 1] + (i & 1);
24+
}
25+
return dp;
26+
}

decode-ways/HC-kang.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// T.C: O(n)
2+
// S.C: O(n)
3+
function numDecodings(s: string): number {
4+
const NUM_OF_ALPHA = 26;
5+
const memo = new Map<number, number>();
6+
7+
function dfs(idx: number): number {
8+
if (idx === s.length) {
9+
return 1;
10+
}
11+
if (s[idx] === '0') {
12+
return 0;
13+
}
14+
if (memo.has(idx)) {
15+
return memo.get(idx)!;
16+
}
17+
18+
let count = dfs(idx + 1);
19+
if (
20+
idx + 2 <= s.length && // check if idx + 2 is in the range
21+
parseInt(s.slice(idx, idx + 2), 10) <= NUM_OF_ALPHA
22+
) {
23+
count += dfs(idx + 2);
24+
}
25+
26+
memo.set(idx, count);
27+
return count;
28+
}
29+
30+
return dfs(0);
31+
}

encode-and-decode-strings/HC-kang.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* The most simple way
3+
*/
4+
function encode(strs: string[]): string {
5+
return strs.join('🏖️');
6+
};
7+
function decode(s: string): string[] {
8+
return s.split('🏖️');
9+
};
10+
11+
// T.C: O(n)
12+
// S.C: O(n)
13+
function encode(strs: string[]): string {
14+
return strs.map((s) => s.length + '#' + s).join('');
15+
}
16+
17+
// T.C: O(n)
18+
// S.C: O(n)
19+
function decode(s: string): string[] {
20+
const res: string[] = [];
21+
let curIdx = 0;
22+
while (curIdx < s.length) {
23+
const sepIdx = s.indexOf('#', curIdx);
24+
const len = parseInt(s.slice(curIdx, sepIdx), 10);
25+
res.push(s.slice(sepIdx + 1, sepIdx + 1 + len));
26+
curIdx = sepIdx + 1 + len;
27+
}
28+
return res;
29+
}

valid-anagram/HC-kang.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// T.C: O(n)
2+
// S.C: O(1)
3+
function isAnagram(s: string, t: string): boolean {
4+
if (s.length !== t.length) return false;
5+
6+
const NUM_OF_ALPHA = 26;
7+
const A_CODE = 'a'.charCodeAt(0);
8+
const bucket = new Array(NUM_OF_ALPHA).fill(0); // S.C: O(1)
9+
10+
for (let i = 0; i < s.length; i++) { // T.C: O(n)
11+
bucket[s.charCodeAt(i) - A_CODE]++;
12+
bucket[t.charCodeAt(i) - A_CODE]--;
13+
}
14+
15+
return bucket.every(count => count === 0);
16+
}

0 commit comments

Comments
 (0)