From 22dcc6162955eff60be946f920b5a0bc86d2930d Mon Sep 17 00:00:00 2001 From: Harsh Vaidya Date: Tue, 2 Jul 2024 22:56:45 +0530 Subject: [PATCH 1/2] add DetectSquare.js --- DetectSquare.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 DetectSquare.js diff --git a/DetectSquare.js b/DetectSquare.js new file mode 100644 index 0000000..ea91f92 --- /dev/null +++ b/DetectSquare.js @@ -0,0 +1,43 @@ +var DetectSquares = function() { + this.points = {}; // Object to store points as key-value pairs +}; + +/** + * @param {number[]} point + * @return {void} + */ +DetectSquares.prototype.add = function(point) { + const [x, y] = point; + const key = `${x},${y}`; // Generating a unique key for each point + this.points[key] = (this.points[key] || 0) + 1; // Incrementing the frequency of the point +}; + +/** + * @param {number[]} point + * @return {number} + */ +DetectSquares.prototype.count = function(point) { + const [x1, y1] = point; + let count = 0; + for (const key in this.points) { + const [x2, y2] = key.split(',').map(Number); // Extracting x and y coordinates from the key + if (x1 !== x2 && y1 !== y2 && Math.abs(x1 - x2) === Math.abs(y1 - y2)) { + // If the points are not on the same axis and form a square + const key2 = `${x1},${y2}`; // Generating the key for the opposite corner of the square + const key3 = `${x2},${y1}`; // Generating the key for the opposite corner of the square + count += (this.points[key] || 0) * (this.points[key2] || 0) * (this.points[key3] || 0); + // Multiplying the frequencies of the points to get the count + } + } + return count; +}; + +// Usage +var obj = new DetectSquares(); +obj.add([3, 10]); +obj.add([11, 2]); +obj.add([3, 2]); +console.log(obj.count([11, 10])); // Output: 1 +console.log(obj.count([14, 8])); // Output: 0 +obj.add([11, 2]); +console.log(obj.count([11, 10])); // Output: 2 From 0709e9d05ad65a6117788c7e2ef97fb1909da6f3 Mon Sep 17 00:00:00 2001 From: Harsh Vaidya Date: Tue, 2 Jul 2024 22:57:48 +0530 Subject: [PATCH 2/2] 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