Skip to content

[hoyeongkwak] Week14 Solutions #1646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions binary-tree-level-order-traversal/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
/*
Time Complexity: O(n)
Space Complexity: O(n)
*/
function levelOrder(root: TreeNode | null): number[][] {
if (root == null) return []
const result: number[][] = []
let queue: TreeNode[] = [root]
while (queue.length > 0) {
const levelSize = queue.length
const currentLevel: number[] = []
for(let i = 0; i< levelSize; i++) {
const node = queue.shift()!
currentLevel.push(node.val)

if (node.left) queue.push(node.left)
if (node.right) queue.push(node.right)
}
result.push(currentLevel)
}
return result
}
11 changes: 11 additions & 0 deletions counting-bits/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Time Complexity: O(n)
Space Complexity: O(n)
*/
function countBits(n: number): number[] {
const result: number[] = new Array(n + 1).fill(0)
for(let i = 1; i <= n; i++) {
result[i] = result[i >> 1] + (i & 1)
}
return result
};
18 changes: 18 additions & 0 deletions house-robber-ii/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Time Complexity: O(n)
Space Complexity: O(1)
*/
function rob(nums: number[]): number {
if (nums.length === 1) return nums[0]
const dp = (start, end): number => {
let prev = 0
let curr = 0
for (let i = start; i < end; i++) {
const temp = curr
curr = Math.max(prev + nums[i], curr)
prev = temp
}
return curr
}
return Math.max(dp(0, nums.length - 1), dp(1, nums.length))
};
83 changes: 83 additions & 0 deletions word-search-ii/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
L : 평균 단어 길이
Time Complexity: O(M * N * 4^L)
Space Complexity: O(∑L + M × N)
*/
class TrieNode {
children: Map<string, TrieNode>
word: string | null

constructor() {
this.children = new Map()
this.word = null
}
}

class Trie {
root: TrieNode
constructor() {
this.root = new TrieNode()
}

insert(word: string): void {
let node = this.root

for (const char of word) {
if (!node.children.has(char)) {
node.children.set(char, new TrieNode())
}
node = node.children.get(char)
}
node.word = word
}
}

function dfs(board: string[][], row: number, col: number, node: TrieNode, visited: boolean[][], result: string[]): void {
const rows = board.length
const cols = board[0].length
if (row < 0 || row >= board.length ||
col < 0 || col >= board[0].length ||
visited[row][col]) {
return
}
const char = board[row][col]
if (!node.children.has(char)) {
return
}
node = node.children.get(char)!

if (node.word !== null) {
result.push(node.word)
node.word = null
}
const originalChar = board[row][col]
visited[row][col] = true
const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]
for (const [dr, dc] of directions) {
dfs(board, row + dr, col + dc, node, visited, result)
}
visited[row][col] = false
}

function findWords(board: string[][], words: string[]): string[] {
if (!board || board.length === 0 || !board[0] || board[0].length === 0) {
return []
}
const result: string[] = []
const trie = new Trie()

for (const word of words) {
trie.insert(word)
}

const rows = board.length
const cols = board[0].length
const visited = Array(rows).fill(null).map(() => Array(cols).fill(false))

for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
dfs(board, i, j, trie.root, visited, result)
}
}
return result
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전 어려워서 풀지 못햇는데 힌드를 얻고 가네요!
감사합니다