-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2013-detect-squares.js
More file actions
54 lines (48 loc) · 1.74 KB
/
2013-detect-squares.js
File metadata and controls
54 lines (48 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* 2013. Detect Squares
* https://leetcode.com/problems/detect-squares/
* Difficulty: Medium
*
* You are given a stream of points on the X-Y plane. Design an algorithm that:
* - Adds new points from the stream into a data structure. Duplicate points are allowed and should
* be treated as different points.
* - Given a query point, counts the number of ways to choose three points from the data structure
* such that the three points and the query point form an axis-aligned square with positive area.
*
* An axis-aligned square is a square whose edges are all the same length and are either parallel
* or perpendicular to the x-axis and y-axis.
*
* Implement the DetectSquares class:
* - DetectSquares() Initializes the object with an empty data structure.
* - void add(int[] point) Adds a new point point = [x, y] to the data structure.
* - int count(int[] point) Counts the number of ways to form axis-aligned squares with point
* point = [x, y] as described above.
*/
var DetectSquares = function() {
this.points = new Map();
};
/**
* @param {number[]} point
* @return {void}
*/
DetectSquares.prototype.add = function(point) {
const [x, y] = point;
const key = `${x},${y}`;
this.points.set(key, (this.points.get(key) || 0) + 1);
};
/**
* @param {number[]} point
* @return {number}
*/
DetectSquares.prototype.count = function(point) {
const [x, y] = point;
let result = 0;
for (const [key, count] of this.points) {
const [px, py] = key.split(',').map(Number);
if (px === x || py === y || Math.abs(px - x) !== Math.abs(py - y)) continue;
const point1 = `${x},${py}`;
const point2 = `${px},${y}`;
result += count * (this.points.get(point1) || 0) * (this.points.get(point2) || 0);
}
return result;
};