diff --git a/binary-tree-level-order-traversal/gwbaik9717.js b/binary-tree-level-order-traversal/gwbaik9717.js new file mode 100644 index 000000000..4bfc40282 --- /dev/null +++ b/binary-tree-level-order-traversal/gwbaik9717.js @@ -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; +}; diff --git a/counting-bits/gwbaik9717.js b/counting-bits/gwbaik9717.js new file mode 100644 index 000000000..6a8681031 --- /dev/null +++ b/counting-bits/gwbaik9717.js @@ -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; +}; diff --git a/house-robber-ii/gwbaik9717.js b/house-robber-ii/gwbaik9717.js new file mode 100644 index 000000000..12b069294 --- /dev/null +++ b/house-robber-ii/gwbaik9717.js @@ -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)); +}; diff --git a/word-search-ii/gwbaik9717.js b/word-search-ii/gwbaik9717.js new file mode 100644 index 000000000..bdc5217d1 --- /dev/null +++ b/word-search-ii/gwbaik9717.js @@ -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]; +};