-
-
Notifications
You must be signed in to change notification settings - Fork 194
[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
+147
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
전 어려워서 풀지 못햇는데 힌드를 얻고 가네요!
감사합니다