diff --git a/climbing-stairs/nhistory.js b/climbing-stairs/nhistory.js new file mode 100644 index 000000000..81dde24af --- /dev/null +++ b/climbing-stairs/nhistory.js @@ -0,0 +1,20 @@ +/** + * @param {number} n + * @return {number} + */ +var climbStairs = function (n) { + // Make an array to store each number of ways + let steps = new Array(n); + // When stairs is 1 and 2 has exact number 1 and 2 + steps[1] = 1; + steps[2] = 2; + // Iterate to get ways of 3 more steps stairs + // ((n-1) + (n-2)) + for (let i = 3; i <= n; i++) { + steps[i] = steps[i - 1] + steps[i - 2]; + } + return steps[n]; +}; + +// TC: O(n) +// SC: O(n) diff --git a/maximum-depth-of-binary-tree/nhistory.js b/maximum-depth-of-binary-tree/nhistory.js new file mode 100644 index 000000000..b521d5c7c --- /dev/null +++ b/maximum-depth-of-binary-tree/nhistory.js @@ -0,0 +1,46 @@ +/** + * 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 maxDepth = function (root) { + // Check root is null or not + if (root === null) { + return 0; + } + // Make queue array to store root and depth variable + let queue = [root]; + let depth = 0; + // Iterate until there is an element inside of queue + while (queue.length > 0) { + // Record level size to avoid fault size measuring + let levelSize = queue.length; + + for (let i = 0; i < levelSize; i++) { + // Grasp first element from the queue + const node = queue.shift(); + // Push the left node into the queue + if (node.left) { + queue.push(node.left); + } + // Push the right node into the queue + if (node.right) { + queue.push(node.right); + } + } + // Increase depth value + depth++; + } + + return depth; +}; + +// TC: O(n) +// SC: O(n) diff --git a/meeting-rooms/nhistory.js b/meeting-rooms/nhistory.js new file mode 100644 index 000000000..0cf517c96 --- /dev/null +++ b/meeting-rooms/nhistory.js @@ -0,0 +1,32 @@ +/** + * Definition of Interval: + * class Interval { + * constructor(start, end) { + * this.start = start; + * this.end = end; + * } + * } + */ + +class Solution { + /** + * @param {Interval[]} intervals + * @returns {boolean} + */ + canAttendMeetings(intervals) { + // Sort the intervals based on their start times + intervals.sort((a, b) => a.start - b.start); + + for (let i = 0; i < intervals.length - 1; i++) { + // Check if the current interval overlaps with the next interval + if (intervals[i].end > intervals[i + 1].start) { + return false; + } + } + + return true; + } +} + +// TC: O(nlogn) +// SC: O(1) diff --git a/same-tree/nhistory.js b/same-tree/nhistory.js new file mode 100644 index 000000000..906aebd14 --- /dev/null +++ b/same-tree/nhistory.js @@ -0,0 +1,24 @@ +/** + * 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} p + * @param {TreeNode} q + * @return {boolean} + */ +var isSameTree = function (p, q) { + // If p and q is null, return true + if (!p && !q) return true; + // Compare root and length between p and q + if (!p || !q || p.val !== q.val) return false; + // Execute recursive function to search each tree + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); +}; + +// TC: O(n) +// SC: O(n) diff --git a/subtree-of-another-tree/nhistory.js b/subtree-of-another-tree/nhistory.js new file mode 100644 index 000000000..4b81a5a14 --- /dev/null +++ b/subtree-of-another-tree/nhistory.js @@ -0,0 +1,39 @@ +/** + * 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 + * @param {TreeNode} subRoot + * @return {boolean} + */ +var isSubtree = function (root, subRoot) { + // Make function to check two triangle is same or not + const isSame = (root1, root2) => { + // If both of root1 and root2 is null, return true + if (!root1 && !root2) return true; + // If one of root1 and root2 is null or root1.val is not equal to root2.val, return false + if (!root1 || !root2 || root1.val !== root2.val) return false; + // Compare each left and right value with recursive + return isSame(root1.left, root2.left) && isSame(root1.right, root2.right); + }; + + // Make dfs function to check nodes inside of root tree + const dfs = (node) => { + // if node is null, return false + if (!node) return false; + // Check triangle is equal to subRoot + if (isSame(node, subRoot)) return true; + // Check one of the left or right node is same with triangle + return dfs(node.left) || dfs(node.right); + }; + // Execute dfs function + return dfs(root); +}; + +// TC: O(n*m) +// SC: O(max(m,n))