diff --git a/binary-tree-level-order-traversal/hsskey.js b/binary-tree-level-order-traversal/hsskey.js new file mode 100644 index 000000000..3c27e593b --- /dev/null +++ b/binary-tree-level-order-traversal/hsskey.js @@ -0,0 +1,40 @@ +/** + * 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 res = []; + if (!root) return res; + + const q = [root]; + + while (q.length > 0) { + const qLen = q.length; + const level = []; + + for (let i = 0; i < qLen; i++) { + const node = q.shift(); + if (node) { + level.push(node.val); + if (node.left) q.push(node.left); + if (node.right) q.push(node.right); + } + } + + if (level.length > 0) { + res.push(level); + } + } + + return res; +}; + diff --git a/counting-bits/hsskey.js b/counting-bits/hsskey.js new file mode 100644 index 000000000..9b0acde26 --- /dev/null +++ b/counting-bits/hsskey.js @@ -0,0 +1,18 @@ +/** + * @param {number} n + * @return {number[]} + */ +var countBits = function(n) { + const dp = new Array(n + 1).fill(0); + let offset = 1; + + for (let i = 1; i <= n; i++) { + if (offset * 2 === i) { + offset = i; + } + dp[i] = 1 + dp[i - offset]; + } + + return dp; +}; + diff --git a/house-robber-ii/hsskey.js b/house-robber-ii/hsskey.js new file mode 100644 index 000000000..e028e20f5 --- /dev/null +++ b/house-robber-ii/hsskey.js @@ -0,0 +1,23 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var rob = function(nums) { + if (nums.length === 1) return nums[0]; + + const helper = (snums) => { + let rob1 = 0, + rob2 = 0; + + for (let n of snums) { + const newRob = Math.max(rob1 + n, rob2); + rob1 = rob2; + rob2 = newRob; + } + + return rob2; + }; + + return Math.max(helper(nums.slice(1)), helper(nums.slice(0, -1))); +}; + diff --git a/meeting-rooms-ii/hsskey.js b/meeting-rooms-ii/hsskey.js new file mode 100644 index 000000000..6850129f1 --- /dev/null +++ b/meeting-rooms-ii/hsskey.js @@ -0,0 +1,31 @@ +export class Solution { + /** + * @param {Interval[]} intervals - an array of meeting time intervals + * @return {number} the minimum number of conference rooms required + */ + minMeetingRooms(intervals) { + if (!intervals || intervals.length === 0) return 0; + + const start = intervals.map(i => i.start).sort((a, b) => a - b); + const end = intervals.map(i => i.end).sort((a, b) => a - b); + + let res = 0; + let count = 0; + let s = 0, + e = 0; + + while (s < intervals.length) { + if (start[s] < end[e]) { + s += 1; + count += 1; + } else { + e += 1; + count -= 1; + } + res = Math.max(res, count); + } + + return res; + } +} + diff --git a/word-search-ii/hsskey.js b/word-search-ii/hsskey.js new file mode 100644 index 000000000..93721aab1 --- /dev/null +++ b/word-search-ii/hsskey.js @@ -0,0 +1,67 @@ +class TrieNode { + constructor() { + this.children = {}; + this.isWord = false; + } + + add(word) { + let node = this; + for (let ch of word) { + if (!node.children[ch]) { + node.children[ch] = new TrieNode(); + } + node = node.children[ch]; + } + node.isWord = true; + } +} + +/** + * @param {character[][]} board + * @param {string[]} words + * @return {string[]} + */ +var findWords = function(board, words) { + const root = new TrieNode(); + for (let word of words) { + root.add(word); + } + + const ROWS = board.length; + const COLS = board[0].length; + const res = new Set(); + const visit = new Set(); + + const dfs = (r, c, node, word) => { + if ( + r < 0 || c < 0 || + r >= ROWS || c >= COLS || + visit.has(`${r},${c}`) || + !node.children[board[r][c]] + ) return; + + visit.add(`${r},${c}`); + node = node.children[board[r][c]]; + word += board[r][c]; + + if (node.isWord) { + res.add(word); + } + + dfs(r + 1, c, node, word); + dfs(r - 1, c, node, word); + dfs(r, c + 1, node, word); + dfs(r, c - 1, node, word); + + visit.delete(`${r},${c}`); + }; + + for (let r = 0; r < ROWS; r++) { + for (let c = 0; c < COLS; c++) { + dfs(r, c, root, ""); + } + } + + return Array.from(res); +}; +