-
-
Couldn't load subscription status.
- Fork 244
[hyer0705] WEEK 14 Solutions #1951
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
+330
−0
Merged
Changes from all commits
Commits
Show all changes
5 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,71 @@ | ||
| /** | ||
| * 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) | ||
| * } | ||
| * } | ||
| */ | ||
|
|
||
| // ----- [Solution 1] | ||
| // Time Complexity: O(n) | ||
| // Space Complexity: O(n) | ||
| function levelOrder(root: TreeNode | null): number[][] { | ||
| if (!root) return []; | ||
|
|
||
| const answer: number[][] = []; | ||
|
|
||
| const queue: [TreeNode, number][] = []; | ||
| let pointer = 0; | ||
|
|
||
| queue.push([root, 0]); | ||
|
|
||
| while (pointer < queue.length) { | ||
| const [currNode, level] = queue[pointer++]; | ||
|
|
||
| if (!answer[level]) answer[level] = []; | ||
|
|
||
| answer[level].push(currNode.val); | ||
|
|
||
| if (currNode.left) queue.push([currNode.left, level + 1]); | ||
| if (currNode.right) queue.push([currNode.right, level + 1]); | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| // ----- [Solution 2] | ||
| // Time Complexity: O(n) | ||
| // Space Complexity: O(n) | ||
| function levelOrder(root: TreeNode | null): number[][] { | ||
| if (!root) return []; | ||
|
|
||
| const answer: number[][] = []; | ||
|
|
||
| const queue: TreeNode[] = []; | ||
| queue.push(root); | ||
|
|
||
| let pointer = 0; | ||
|
|
||
| while (pointer < queue.length) { | ||
| const levelSize = queue.length - pointer; | ||
| const currentLevel: number[] = []; | ||
|
|
||
| for (let i = 0; i < levelSize; i++) { | ||
| const currNode = queue[pointer++]; | ||
| currentLevel.push(currNode.val); | ||
|
|
||
| if (currNode.left) queue.push(currNode.left); | ||
| if (currNode.right) queue.push(currNode.right); | ||
| } | ||
|
|
||
| answer.push(currentLevel); | ||
| } | ||
|
|
||
| return answer; | ||
| } |
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 ans: number[] = Array.from({ length: n + 1 }, () => 0); | ||
|
|
||
| for (let i = 1; i <= n; i++) { | ||
| ans[i] = ans[i >> 1] + (i & 1); | ||
| } | ||
|
|
||
| return ans; | ||
| } | ||
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,26 @@ | ||
| // Time Complexity: O(n) | ||
| // Space Complexity: O(1) | ||
| function rob(nums: number[]): number { | ||
| const n = nums.length; | ||
|
|
||
| if (n === 1) return nums[0]; | ||
| if (n === 2) return Math.max(nums[0], nums[1]); | ||
|
|
||
| const getLinearRob = (start: number, end: number) => { | ||
| let prev2 = 0; | ||
| let prev1 = 0; | ||
|
|
||
| for (let i = start; i <= end; i++) { | ||
| const current = Math.max(prev1, prev2 + nums[i]); | ||
| prev2 = prev1; | ||
| prev1 = current; | ||
| } | ||
|
|
||
| return prev1; | ||
| }; | ||
|
|
||
| let case1 = getLinearRob(0, n - 2); | ||
| let case2 = getLinearRob(1, n - 1); | ||
|
|
||
| return Math.max(case1, case2); | ||
| } |
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,46 @@ | ||
| import { Interval } from "../home/lib/index"; | ||
| /** | ||
| * Definition of Interval: | ||
| * export class Interval { | ||
| * start :number; | ||
| * end :number; | ||
| * constructor(start :number, end :number) { | ||
| * this.start = start; | ||
| * this.end = end; | ||
| * } | ||
| * } | ||
| */ | ||
| import { MinHeap } from "@datastructures-js/heap"; | ||
|
|
||
| export class Solution { | ||
| /** | ||
| * @param intervals: an array of meeting time intervals | ||
| * @return: the minimum number of conference rooms required | ||
| */ | ||
| // Time Complexity: O(n log n) | ||
| // Space Complexity: O(n) | ||
| minMeetingRooms(intervals: Interval[]): number { | ||
| intervals.sort((a, b) => a.start - b.start); | ||
|
|
||
| const minHeap = new MinHeap(); | ||
| const n = intervals.length; | ||
|
|
||
| let maximumRooms = 0; | ||
| for (let i = 0; i < n; i++) { | ||
| const { start, end } = intervals[i]; | ||
|
|
||
| if (minHeap.isEmpty()) { | ||
| minHeap.insert(end); | ||
| maximumRooms = Math.max(maximumRooms, minHeap.size()); | ||
| continue; | ||
| } | ||
|
|
||
| if (start >= minHeap.root()) { | ||
| minHeap.pop(); | ||
| } | ||
| minHeap.insert(end); | ||
| maximumRooms = Math.max(maximumRooms, minHeap.size()); | ||
| } | ||
| return maximumRooms; | ||
| } | ||
| } |
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,176 @@ | ||
| // 84ms | ||
| class TrieNode { | ||
| children: Map<string, TrieNode>; | ||
| isEndOf: boolean; | ||
| word: string | null; | ||
|
|
||
| constructor() { | ||
| this.children = new Map(); | ||
| this.word = null; | ||
| } | ||
| } | ||
| class Trie { | ||
| root: TrieNode; | ||
|
|
||
| constructor() { | ||
| this.root = new TrieNode(); | ||
| } | ||
|
|
||
| insert(word: string) { | ||
| let current = this.root; | ||
|
|
||
| for (const char of word) { | ||
| if (!current.children.has(char)) { | ||
| current.children.set(char, new TrieNode()); | ||
| } | ||
| current = current.children.get(char); | ||
| } | ||
|
|
||
| current.word = word; | ||
| } | ||
| } | ||
|
|
||
| function findWords(board: string[][], words: string[]): string[] { | ||
| const m = board.length; | ||
| const n = board[0].length; | ||
|
|
||
| const dictionary = new Trie(); | ||
| for (const word of words) { | ||
| dictionary.insert(word); | ||
| } | ||
|
|
||
| const results: string[] = []; | ||
|
|
||
| const dfs = (row: number, col: number, trieNode: TrieNode) => { | ||
| if (trieNode.word !== null) { | ||
| results.push(trieNode.word); | ||
| trieNode.word = null; | ||
| } | ||
|
|
||
| const directions = [ | ||
| [-1, 0], | ||
| [1, 0], | ||
| [0, -1], | ||
| [0, 1], | ||
| ]; | ||
|
|
||
| for (const [dr, dc] of directions) { | ||
| const [nr, nc] = [row + dr, col + dc]; | ||
|
|
||
| if (nr >= 0 && nr < m && nc >= 0 && nc < n) { | ||
| if (trieNode.children.has(board[nr][nc])) { | ||
| const char = board[nr][nc]; | ||
| const childNode = trieNode.children.get(char); | ||
|
|
||
| if (board[nr][nc] !== "#") { | ||
| board[nr][nc] = "#"; | ||
| dfs(nr, nc, trieNode.children.get(char)); | ||
| board[nr][nc] = char; | ||
| } | ||
|
|
||
| if (childNode.children.size === 0 && childNode.word === null) { | ||
| trieNode.children.delete(char); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| for (let i = 0; i < m; i++) { | ||
| for (let j = 0; j < n; j++) { | ||
| const firstChar = board[i][j]; | ||
| if (dictionary.root.children.has(firstChar)) { | ||
| board[i][j] = "#"; | ||
| dfs(i, j, dictionary.root.children.get(firstChar)); | ||
| board[i][j] = firstChar; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| // 2312ms | ||
| 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) { | ||
| let current = this.root; | ||
|
|
||
| for (const char of word) { | ||
| if (!current.children.has(char)) { | ||
| current.children.set(char, new TrieNode()); | ||
| } | ||
| current = current.children.get(char)!; | ||
| } | ||
|
|
||
| current.word = word; | ||
| } | ||
| } | ||
|
|
||
| function findWords(board: string[][], words: string[]): string[] { | ||
| const m = board.length; | ||
| const n = board[0].length; | ||
|
|
||
| const dictionary = new Trie(); | ||
| for (const word of words) { | ||
| dictionary.insert(word); | ||
| } | ||
|
|
||
| const results: string[] = []; | ||
|
|
||
| const dfs = (row: number, col: number, trieNode: TrieNode, path: string) => { | ||
| if (trieNode.word === path) { | ||
| results.push(path); | ||
| trieNode.word = null; | ||
| } | ||
|
|
||
| const directions = [ | ||
| [-1, 0], | ||
| [1, 0], | ||
| [0, -1], | ||
| [0, 1], | ||
| ]; | ||
|
|
||
| for (const [dr, dc] of directions) { | ||
| const [nr, nc] = [row + dr, col + dc]; | ||
|
|
||
| if (nr >= 0 && nr < m && nc >= 0 && nc < n) { | ||
| if (trieNode.children.has(board[nr][nc])) { | ||
| if (!isVisited.has(`${nr},${nc}`)) { | ||
| isVisited.add(`${nr},${nc}`); | ||
| dfs(nr, nc, trieNode.children.get(board[nr][nc])!, path + board[nr][nc]); | ||
| isVisited.delete(`${nr},${nc}`); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const isVisited = new Set<string>(); // 'r,c' | ||
| for (let i = 0; i < m; i++) { | ||
| for (let j = 0; j < n; j++) { | ||
| const firstChar = board[i][j]; | ||
| if (dictionary.root.children.has(firstChar)) { | ||
| isVisited.add(`${i},${j}`); | ||
| dfs(i, j, dictionary.root.children.get(firstChar)!, firstChar); | ||
| isVisited.delete(`${i},${j}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return results; | ||
| } |
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.
비트연산자를 이용하셨네요. 이게 정석인 것 같은데 저는 이진수로 바꿔서 1 개수를 카운팅해서 풀어서..
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.
비트 연산자에 익숙하지 않다면 이진수로 바꿔서 1의 갯수를 카운팅하는 방법도 좋은 방법이라고 생각합니다!