Skip to content

Commit 5a776b6

Browse files
authored
Merge pull request #363 from whewchews/main
[pepper] WEEK 2 Solution
2 parents 81d4e12 + 6843903 commit 5a776b6

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
* preorder: [root, left, right]
14+
* inorder: [left, root, right]
15+
* preorder의 첫번째 값은 root이다. (1. 현재 root 찾기)
16+
* 모든 node의 val는 unique하기 때문에 이 값을 기준으로 inorder에서 root의 위치를 찾을 수 있다.
17+
*
18+
* inorder에서 root의 위치를 찾으면, root를 기준으로 왼쪽은 left subtree, 오른쪽은 right subtree이다. (2. left subtree, right subtree 구분)
19+
* inorder: [...left, root, ...right]
20+
* root값은 이미 찾았기 때문에 shift로 제거한다.
21+
*
22+
* 남은 preorder에서 첫번째 값은 left subtree의 root이다. (3. left subtree 구성)
23+
* preorder에서 하나씩 shift하면서 왼쪽 트리를 먼저 구성한다.
24+
* preorder에서 첫번째 값이 왼쪽 subtree의 root이다. (1. 현재 root 찾기)
25+
* inorder에서 root의 위치를 찾아서 왼쪽 subtree를 구성한다. (2. left subtree, right subtree 구분) (3. left subtree 구성)
26+
* root 기준 왼쪽 subtree 구성이 끝나면 오른쪽 subtree를 구성한다.
27+
* 위 과정을 재귀적으로 반복하면, 전체 트리를 구성할 수 있다. (1-3 과정 반복)
28+
*/
29+
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
30+
// build 함수가 각 노드마다 호출됨(N) * 각 노드마다 shift, indexOf 수행(N) = O(N^2)
31+
function build(preorder, inorder) {
32+
if (inorder.length) {
33+
// TC: O(N)
34+
const idx = inorder.indexOf(preorder.shift());
35+
const root = new TreeNode(inorder[idx]);
36+
37+
root.left = build(preorder, inorder.slice(0, idx));
38+
root.right = build(preorder, inorder.slice(idx + 1));
39+
40+
return root;
41+
}
42+
return null;
43+
}
44+
45+
return build(preorder, inorder);
46+
}
47+
48+
// TC: O(N^2)
49+
// SC: O(N^2)

counting-bits/whewchews.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function countBits(n: number): number[] {
2+
// SC: O(N)
3+
const ans = Array(n + 1).fill(0);
4+
// TC: O(N)
5+
for (let i = 1; i <= n; i++) {
6+
let k = i;
7+
8+
// TC: O(log N)
9+
while (k > 0) {
10+
ans[i] += k % 2;
11+
k = Math.floor(k / 2);
12+
}
13+
}
14+
15+
return ans;
16+
}
17+
18+
// TC: O(N log N)
19+
// SC: O(N)

decode-ways/whewchews.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function numDecodings(s: string): number {
2+
// SC: O(N)
3+
const memo: { [key: number]: number } = { [s.length]: 1 };
4+
5+
// TC: O(N)
6+
const dfs = (start: number): number => {
7+
if (start in memo) {
8+
return memo[start];
9+
}
10+
11+
if (s[start] === "0") {
12+
// 0으로 시작하는 경우 가능한 경우의 수가 없음
13+
memo[start] = 0;
14+
} else if (
15+
start + 1 < s.length &&
16+
parseInt(s.substring(start, start + 2)) < 27
17+
) {
18+
// 다음에 오는 글자가 두글자 이상 있고, start start+1 두글자가 1~26 사이의 값인 경우
19+
memo[start] = dfs(start + 1) + dfs(start + 2);
20+
} else {
21+
// 1글자만 남은 경우 or 첫 두글자가 27보다 큰 경우
22+
memo[start] = dfs(start + 1);
23+
}
24+
25+
return memo[start];
26+
};
27+
28+
// SC: 재귀호출 O(N)
29+
return dfs(0);
30+
}
31+
32+
// TC: O(N)
33+
// SC: O(N)

valid-anagram/whewchews.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function isAnagram(s: string, t: string): boolean {
2+
// TC: O(1)
3+
if (s.length !== t.length) return false;
4+
5+
// SC: O(N)
6+
const count: { [key: string]: number } = {};
7+
8+
// TC: O(N)
9+
for (let i = 0; i <= s.length - 1; i++) {
10+
const sChar = s[i];
11+
const tChar = t[i];
12+
count[sChar] = (count[sChar] ?? 0) + 1;
13+
count[tChar] = (count[tChar] ?? 0) - 1;
14+
}
15+
16+
// TC: O(N)
17+
return Object.values(count).every((v) => v === 0);
18+
}
19+
20+
// TC: O(N)
21+
// SC: O(N)

0 commit comments

Comments
 (0)