Skip to content

Commit

Permalink
add eraseOverlapIntervals.js
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh432004 committed Jul 2, 2024
1 parent 22dcc61 commit 0709e9d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
32 changes: 32 additions & 0 deletions diameterOfBinaryTree.js
Original file line number Diff line number Diff line change
@@ -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
};
24 changes: 24 additions & 0 deletions eraseOverlapIntervals.js
Original file line number Diff line number Diff line change
@@ -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<intervals.length;i++){
const [start,end] = intervals[i]

if(start < lastNonOverlappingEnd){
removed++
}else{
lastNonOverlappingEnd = end
}
}
return removed

};

0 comments on commit 0709e9d

Please sign in to comment.