-
-
Notifications
You must be signed in to change notification settings - Fork 195
[ganu] Week 14 #1098
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
[ganu] Week 14 #1098
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,67 @@ | ||
// n: number of nodes | ||
// Time complexity: O(n) | ||
// Space complexity: O(n) | ||
|
||
class _Queue { | ||
constructor() { | ||
this.q = []; | ||
this.front = 0; | ||
this.rear = 0; | ||
} | ||
|
||
push(value) { | ||
this.q.push(value); | ||
this.rear++; | ||
} | ||
|
||
shift() { | ||
const rv = this.q[this.front]; | ||
delete this.q[this.front++]; | ||
return rv; | ||
} | ||
|
||
isEmpty() { | ||
return this.front === this.rear; | ||
} | ||
} | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val, left, right) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} root | ||
* @return {number[][]} | ||
*/ | ||
var levelOrder = function (root) { | ||
const answer = []; | ||
const q = new _Queue(); | ||
|
||
if (root) { | ||
q.push([root, 0]); | ||
} | ||
|
||
while (!q.isEmpty()) { | ||
const [current, lv] = q.shift(); | ||
|
||
if (answer.at(lv) === undefined) { | ||
answer[lv] = []; | ||
} | ||
|
||
answer[lv].push(current.val); | ||
|
||
if (current.left) { | ||
q.push([current.left, lv + 1]); | ||
} | ||
|
||
if (current.right) { | ||
q.push([current.right, lv + 1]); | ||
} | ||
} | ||
|
||
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,24 @@ | ||
// Time complexity: O(n) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* @param {number} n | ||
* @return {number[]} | ||
*/ | ||
var countBits = function (n) { | ||
const dp = Array.from({ length: n + 1 }, () => 0); | ||
|
||
if (n === 0) { | ||
return dp; | ||
} | ||
|
||
dp[1] = 1; | ||
|
||
for (let i = 2; i <= n; i++) { | ||
const k = Math.floor(Math.log2(i)); | ||
|
||
dp[i] = 1 + dp[i - Math.pow(2, k)]; | ||
} | ||
|
||
return dp; | ||
}; |
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(n) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var rob = function (nums) { | ||
if (nums.length === 1) { | ||
return nums[0]; | ||
} | ||
|
||
// include first | ||
const dp1 = Array.from({ length: nums.length + 1 }, () => 0); | ||
dp1[1] = nums[0]; | ||
|
||
// exclude first | ||
const dp2 = Array.from({ length: nums.length + 1 }, () => 0); | ||
|
||
for (let i = 2; i <= nums.length; i++) { | ||
dp1[i] = Math.max(dp1[i - 2] + nums[i - 1], dp1[i - 1]); | ||
dp2[i] = Math.max(dp2[i - 2] + nums[i - 1], dp2[i - 1]); | ||
} | ||
|
||
return Math.max(dp1.at(-2), dp2.at(-1)); | ||
}; |
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,87 @@ | ||
// k: length of word, h: height of board, w: width of board | ||
// Time complexity: O(4^k * h * w) | ||
// Space complexity: O(4^k) | ||
|
||
class Node { | ||
constructor(value = "") { | ||
this.value = value; | ||
this.children = new Map(); | ||
this.isEnd = false; | ||
} | ||
} | ||
class Trie { | ||
constructor() { | ||
this.head = new Node(); | ||
} | ||
|
||
add(str) { | ||
let current = this.head; | ||
|
||
for (const chr of str) { | ||
if (!current.children.has(chr)) { | ||
current.children.set(chr, new Node(current.value + chr)); | ||
} | ||
|
||
current = current.children.get(chr); | ||
} | ||
|
||
current.isEnd = true; | ||
} | ||
} | ||
|
||
/** | ||
* @param {character[][]} board | ||
* @param {string[]} words | ||
* @return {string[]} | ||
*/ | ||
var findWords = function (board, words) { | ||
const answer = new Set(); | ||
|
||
const h = board.length; | ||
const w = board[0].length; | ||
|
||
const dy = [1, 0, -1, 0]; | ||
const dx = [0, 1, 0, -1]; | ||
const checked = Array.from({ length: h }, () => | ||
Array.from({ length: w }, () => false) | ||
); | ||
|
||
const dfs = (current, children) => { | ||
const [cy, cx] = current; | ||
|
||
if (!children.has(board[cy][cx])) { | ||
return; | ||
} | ||
|
||
if (children.get(board[cy][cx]).isEnd) { | ||
answer.add(children.get(board[cy][cx]).value); | ||
} | ||
|
||
for (let j = 0; j < dx.length; j++) { | ||
const nx = cx + dx[j]; | ||
const ny = cy + dy[j]; | ||
|
||
if (nx >= 0 && nx < w && ny >= 0 && ny < h && !checked[ny][nx]) { | ||
checked[ny][nx] = true; | ||
dfs([ny, nx], children.get(board[cy][cx]).children); | ||
checked[ny][nx] = false; | ||
} | ||
} | ||
}; | ||
|
||
const trie = new Trie(); | ||
|
||
for (const word of words) { | ||
trie.add(word); | ||
} | ||
|
||
for (let i = 0; i < h; i++) { | ||
for (let j = 0; j < w; j++) { | ||
checked[i][j] = true; | ||
dfs([i, j], trie.head.children); | ||
checked[i][j] = false; | ||
} | ||
} | ||
|
||
return [...answer]; | ||
}; |
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.
클래스를 아예 만드셔서 푸시다니... 존경합니다.