From 0709e9d05ad65a6117788c7e2ef97fb1909da6f3 Mon Sep 17 00:00:00 2001 From: Harsh Vaidya Date: Tue, 2 Jul 2024 22:57:48 +0530 Subject: [PATCH] add eraseOverlapIntervals.js --- diameterOfBinaryTree.js | 32 ++++++++++++++++++++++++++++++++ eraseOverlapIntervals.js | 24 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 diameterOfBinaryTree.js create mode 100644 eraseOverlapIntervals.js diff --git a/diameterOfBinaryTree.js b/diameterOfBinaryTree.js new file mode 100644 index 0000000..f880a36 --- /dev/null +++ b/diameterOfBinaryTree.js @@ -0,0 +1,32 @@ +/** + * 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 diameterOfBinaryTree = function(root) { + let maxD= 0 + + function dfs(node){ + if(!node) return 0; + + let left = dfs(node.left); + let right = dfs(node.right); + let currD = left + right + + maxD = Math.max(currD,maxD) + + return Math.max(left,right)+1 + + } + + dfs(root) + + return maxD +}; \ No newline at end of file diff --git a/eraseOverlapIntervals.js b/eraseOverlapIntervals.js new file mode 100644 index 0000000..99c1ed1 --- /dev/null +++ b/eraseOverlapIntervals.js @@ -0,0 +1,24 @@ +/** + * @param {number[][]} intervals + * @return {number} + */ +var eraseOverlapIntervals = function(intervals) { + if(intervals.length === 0) return 0 + + intervals.sort((a,b) => a[1] - b[1]) + + let removed =0 + let lastNonOverlappingEnd = intervals[0][1] + + for(let i=1;i