Skip to content

Commit 8abf1b9

Browse files
authored
Merge pull request #1646 from hoyeongkwak/main
[hoyeongkwak] Week14 Solutions
2 parents 2c7c2f8 + 242bb93 commit 8abf1b9

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
/*
15+
Time Complexity: O(n)
16+
Space Complexity: O(n)
17+
*/
18+
function levelOrder(root: TreeNode | null): number[][] {
19+
if (root == null) return []
20+
const result: number[][] = []
21+
let queue: TreeNode[] = [root]
22+
while (queue.length > 0) {
23+
const levelSize = queue.length
24+
const currentLevel: number[] = []
25+
for(let i = 0; i< levelSize; i++) {
26+
const node = queue.shift()!
27+
currentLevel.push(node.val)
28+
29+
if (node.left) queue.push(node.left)
30+
if (node.right) queue.push(node.right)
31+
}
32+
result.push(currentLevel)
33+
}
34+
return result
35+
}

counting-bits/hoyeongkwak.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Time Complexity: O(n)
3+
Space Complexity: O(n)
4+
*/
5+
function countBits(n: number): number[] {
6+
const result: number[] = new Array(n + 1).fill(0)
7+
for(let i = 1; i <= n; i++) {
8+
result[i] = result[i >> 1] + (i & 1)
9+
}
10+
return result
11+
};

house-robber-ii/hoyeongkwak.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Time Complexity: O(n)
3+
Space Complexity: O(1)
4+
*/
5+
function rob(nums: number[]): number {
6+
if (nums.length === 1) return nums[0]
7+
const dp = (start, end): number => {
8+
let prev = 0
9+
let curr = 0
10+
for (let i = start; i < end; i++) {
11+
const temp = curr
12+
curr = Math.max(prev + nums[i], curr)
13+
prev = temp
14+
}
15+
return curr
16+
}
17+
return Math.max(dp(0, nums.length - 1), dp(1, nums.length))
18+
};

word-search-ii/hoyeongkwak.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
L : 평균 단어 길이
3+
Time Complexity: O(M * N * 4^L)
4+
Space Complexity: O(∑L + M × N)
5+
*/
6+
class TrieNode {
7+
children: Map<string, TrieNode>
8+
word: string | null
9+
10+
constructor() {
11+
this.children = new Map()
12+
this.word = null
13+
}
14+
}
15+
16+
class Trie {
17+
root: TrieNode
18+
constructor() {
19+
this.root = new TrieNode()
20+
}
21+
22+
insert(word: string): void {
23+
let node = this.root
24+
25+
for (const char of word) {
26+
if (!node.children.has(char)) {
27+
node.children.set(char, new TrieNode())
28+
}
29+
node = node.children.get(char)
30+
}
31+
node.word = word
32+
}
33+
}
34+
35+
function dfs(board: string[][], row: number, col: number, node: TrieNode, visited: boolean[][], result: string[]): void {
36+
const rows = board.length
37+
const cols = board[0].length
38+
if (row < 0 || row >= board.length ||
39+
col < 0 || col >= board[0].length ||
40+
visited[row][col]) {
41+
return
42+
}
43+
const char = board[row][col]
44+
if (!node.children.has(char)) {
45+
return
46+
}
47+
node = node.children.get(char)!
48+
49+
if (node.word !== null) {
50+
result.push(node.word)
51+
node.word = null
52+
}
53+
const originalChar = board[row][col]
54+
visited[row][col] = true
55+
const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]
56+
for (const [dr, dc] of directions) {
57+
dfs(board, row + dr, col + dc, node, visited, result)
58+
}
59+
visited[row][col] = false
60+
}
61+
62+
function findWords(board: string[][], words: string[]): string[] {
63+
if (!board || board.length === 0 || !board[0] || board[0].length === 0) {
64+
return []
65+
}
66+
const result: string[] = []
67+
const trie = new Trie()
68+
69+
for (const word of words) {
70+
trie.insert(word)
71+
}
72+
73+
const rows = board.length
74+
const cols = board[0].length
75+
const visited = Array(rows).fill(null).map(() => Array(cols).fill(false))
76+
77+
for (let i = 0; i < rows; i++) {
78+
for (let j = 0; j < cols; j++) {
79+
dfs(board, i, j, trie.root, visited, result)
80+
}
81+
}
82+
return result
83+
};

0 commit comments

Comments
 (0)