Skip to content

Commit 6519d16

Browse files
authored
Merge pull request #1098 from gwbaik9717/main
[ganu] Week 14
2 parents 980a8c1 + cec6d45 commit 6519d16

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// n: number of nodes
2+
// Time complexity: O(n)
3+
// Space complexity: O(n)
4+
5+
class _Queue {
6+
constructor() {
7+
this.q = [];
8+
this.front = 0;
9+
this.rear = 0;
10+
}
11+
12+
push(value) {
13+
this.q.push(value);
14+
this.rear++;
15+
}
16+
17+
shift() {
18+
const rv = this.q[this.front];
19+
delete this.q[this.front++];
20+
return rv;
21+
}
22+
23+
isEmpty() {
24+
return this.front === this.rear;
25+
}
26+
}
27+
28+
/**
29+
* Definition for a binary tree node.
30+
* function TreeNode(val, left, right) {
31+
* this.val = (val===undefined ? 0 : val)
32+
* this.left = (left===undefined ? null : left)
33+
* this.right = (right===undefined ? null : right)
34+
* }
35+
*/
36+
/**
37+
* @param {TreeNode} root
38+
* @return {number[][]}
39+
*/
40+
var levelOrder = function (root) {
41+
const answer = [];
42+
const q = new _Queue();
43+
44+
if (root) {
45+
q.push([root, 0]);
46+
}
47+
48+
while (!q.isEmpty()) {
49+
const [current, lv] = q.shift();
50+
51+
if (answer.at(lv) === undefined) {
52+
answer[lv] = [];
53+
}
54+
55+
answer[lv].push(current.val);
56+
57+
if (current.left) {
58+
q.push([current.left, lv + 1]);
59+
}
60+
61+
if (current.right) {
62+
q.push([current.right, lv + 1]);
63+
}
64+
}
65+
66+
return answer;
67+
};

counting-bits/gwbaik9717.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* @param {number} n
6+
* @return {number[]}
7+
*/
8+
var countBits = function (n) {
9+
const dp = Array.from({ length: n + 1 }, () => 0);
10+
11+
if (n === 0) {
12+
return dp;
13+
}
14+
15+
dp[1] = 1;
16+
17+
for (let i = 2; i <= n; i++) {
18+
const k = Math.floor(Math.log2(i));
19+
20+
dp[i] = 1 + dp[i - Math.pow(2, k)];
21+
}
22+
23+
return dp;
24+
};

house-robber-ii/gwbaik9717.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var rob = function (nums) {
9+
if (nums.length === 1) {
10+
return nums[0];
11+
}
12+
13+
// include first
14+
const dp1 = Array.from({ length: nums.length + 1 }, () => 0);
15+
dp1[1] = nums[0];
16+
17+
// exclude first
18+
const dp2 = Array.from({ length: nums.length + 1 }, () => 0);
19+
20+
for (let i = 2; i <= nums.length; i++) {
21+
dp1[i] = Math.max(dp1[i - 2] + nums[i - 1], dp1[i - 1]);
22+
dp2[i] = Math.max(dp2[i - 2] + nums[i - 1], dp2[i - 1]);
23+
}
24+
25+
return Math.max(dp1.at(-2), dp2.at(-1));
26+
};

word-search-ii/gwbaik9717.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// k: length of word, h: height of board, w: width of board
2+
// Time complexity: O(4^k * h * w)
3+
// Space complexity: O(4^k)
4+
5+
class Node {
6+
constructor(value = "") {
7+
this.value = value;
8+
this.children = new Map();
9+
this.isEnd = false;
10+
}
11+
}
12+
class Trie {
13+
constructor() {
14+
this.head = new Node();
15+
}
16+
17+
add(str) {
18+
let current = this.head;
19+
20+
for (const chr of str) {
21+
if (!current.children.has(chr)) {
22+
current.children.set(chr, new Node(current.value + chr));
23+
}
24+
25+
current = current.children.get(chr);
26+
}
27+
28+
current.isEnd = true;
29+
}
30+
}
31+
32+
/**
33+
* @param {character[][]} board
34+
* @param {string[]} words
35+
* @return {string[]}
36+
*/
37+
var findWords = function (board, words) {
38+
const answer = new Set();
39+
40+
const h = board.length;
41+
const w = board[0].length;
42+
43+
const dy = [1, 0, -1, 0];
44+
const dx = [0, 1, 0, -1];
45+
const checked = Array.from({ length: h }, () =>
46+
Array.from({ length: w }, () => false)
47+
);
48+
49+
const dfs = (current, children) => {
50+
const [cy, cx] = current;
51+
52+
if (!children.has(board[cy][cx])) {
53+
return;
54+
}
55+
56+
if (children.get(board[cy][cx]).isEnd) {
57+
answer.add(children.get(board[cy][cx]).value);
58+
}
59+
60+
for (let j = 0; j < dx.length; j++) {
61+
const nx = cx + dx[j];
62+
const ny = cy + dy[j];
63+
64+
if (nx >= 0 && nx < w && ny >= 0 && ny < h && !checked[ny][nx]) {
65+
checked[ny][nx] = true;
66+
dfs([ny, nx], children.get(board[cy][cx]).children);
67+
checked[ny][nx] = false;
68+
}
69+
}
70+
};
71+
72+
const trie = new Trie();
73+
74+
for (const word of words) {
75+
trie.add(word);
76+
}
77+
78+
for (let i = 0; i < h; i++) {
79+
for (let j = 0; j < w; j++) {
80+
checked[i][j] = true;
81+
dfs([i, j], trie.head.children);
82+
checked[i][j] = false;
83+
}
84+
}
85+
86+
return [...answer];
87+
};

0 commit comments

Comments
 (0)