Skip to content

Commit 3ccf512

Browse files
authored
Merge pull request #1645 from hsskey/main
[hsskey] Week 14 Solutions
2 parents 7a49bf6 + 08324ac commit 3ccf512

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val === undefined ? 0 : val);
5+
* this.left = (left === undefined ? null : left);
6+
* this.right = (right === undefined ? null : right);
7+
* }
8+
*/
9+
10+
/**
11+
* @param {TreeNode} root
12+
* @return {number[][]}
13+
*/
14+
var levelOrder = function(root) {
15+
const res = [];
16+
if (!root) return res;
17+
18+
const q = [root];
19+
20+
while (q.length > 0) {
21+
const qLen = q.length;
22+
const level = [];
23+
24+
for (let i = 0; i < qLen; i++) {
25+
const node = q.shift();
26+
if (node) {
27+
level.push(node.val);
28+
if (node.left) q.push(node.left);
29+
if (node.right) q.push(node.right);
30+
}
31+
}
32+
33+
if (level.length > 0) {
34+
res.push(level);
35+
}
36+
}
37+
38+
return res;
39+
};
40+

counting-bits/hsskey.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number} n
3+
* @return {number[]}
4+
*/
5+
var countBits = function(n) {
6+
const dp = new Array(n + 1).fill(0);
7+
let offset = 1;
8+
9+
for (let i = 1; i <= n; i++) {
10+
if (offset * 2 === i) {
11+
offset = i;
12+
}
13+
dp[i] = 1 + dp[i - offset];
14+
}
15+
16+
return dp;
17+
};
18+

house-robber-ii/hsskey.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var rob = function(nums) {
6+
if (nums.length === 1) return nums[0];
7+
8+
const helper = (snums) => {
9+
let rob1 = 0,
10+
rob2 = 0;
11+
12+
for (let n of snums) {
13+
const newRob = Math.max(rob1 + n, rob2);
14+
rob1 = rob2;
15+
rob2 = newRob;
16+
}
17+
18+
return rob2;
19+
};
20+
21+
return Math.max(helper(nums.slice(1)), helper(nums.slice(0, -1)));
22+
};
23+

meeting-rooms-ii/hsskey.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export class Solution {
2+
/**
3+
* @param {Interval[]} intervals - an array of meeting time intervals
4+
* @return {number} the minimum number of conference rooms required
5+
*/
6+
minMeetingRooms(intervals) {
7+
if (!intervals || intervals.length === 0) return 0;
8+
9+
const start = intervals.map(i => i.start).sort((a, b) => a - b);
10+
const end = intervals.map(i => i.end).sort((a, b) => a - b);
11+
12+
let res = 0;
13+
let count = 0;
14+
let s = 0,
15+
e = 0;
16+
17+
while (s < intervals.length) {
18+
if (start[s] < end[e]) {
19+
s += 1;
20+
count += 1;
21+
} else {
22+
e += 1;
23+
count -= 1;
24+
}
25+
res = Math.max(res, count);
26+
}
27+
28+
return res;
29+
}
30+
}
31+

word-search-ii/hsskey.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class TrieNode {
2+
constructor() {
3+
this.children = {};
4+
this.isWord = false;
5+
}
6+
7+
add(word) {
8+
let node = this;
9+
for (let ch of word) {
10+
if (!node.children[ch]) {
11+
node.children[ch] = new TrieNode();
12+
}
13+
node = node.children[ch];
14+
}
15+
node.isWord = true;
16+
}
17+
}
18+
19+
/**
20+
* @param {character[][]} board
21+
* @param {string[]} words
22+
* @return {string[]}
23+
*/
24+
var findWords = function(board, words) {
25+
const root = new TrieNode();
26+
for (let word of words) {
27+
root.add(word);
28+
}
29+
30+
const ROWS = board.length;
31+
const COLS = board[0].length;
32+
const res = new Set();
33+
const visit = new Set();
34+
35+
const dfs = (r, c, node, word) => {
36+
if (
37+
r < 0 || c < 0 ||
38+
r >= ROWS || c >= COLS ||
39+
visit.has(`${r},${c}`) ||
40+
!node.children[board[r][c]]
41+
) return;
42+
43+
visit.add(`${r},${c}`);
44+
node = node.children[board[r][c]];
45+
word += board[r][c];
46+
47+
if (node.isWord) {
48+
res.add(word);
49+
}
50+
51+
dfs(r + 1, c, node, word);
52+
dfs(r - 1, c, node, word);
53+
dfs(r, c + 1, node, word);
54+
dfs(r, c - 1, node, word);
55+
56+
visit.delete(`${r},${c}`);
57+
};
58+
59+
for (let r = 0; r < ROWS; r++) {
60+
for (let c = 0; c < COLS; c++) {
61+
dfs(r, c, root, "");
62+
}
63+
}
64+
65+
return Array.from(res);
66+
};
67+

0 commit comments

Comments
 (0)