Skip to content

[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 5 commits into from
Mar 15, 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
67 changes: 67 additions & 0 deletions binary-tree-level-order-traversal/gwbaik9717.js
Copy link
Contributor

Choose a reason for hiding this comment

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

클래스를 아예 만드셔서 푸시다니... 존경합니다.

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;
};
24 changes: 24 additions & 0 deletions counting-bits/gwbaik9717.js
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;
};
26 changes: 26 additions & 0 deletions house-robber-ii/gwbaik9717.js
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));
};
87 changes: 87 additions & 0 deletions word-search-ii/gwbaik9717.js
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];
};