Skip to content

Commit 5632cf9

Browse files
authored
Update 2013-detect-squares.js
1 parent 5375fee commit 5632cf9

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

2013-detect-squares.js

+42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,46 @@
11

2+
const DetectSquares = function() {
3+
this.pts = []
4+
this.ptsCnt = {}
5+
};
6+
7+
/**
8+
* @param {number[]} point
9+
* @return {void}
10+
*/
11+
DetectSquares.prototype.add = function(point) {
12+
this.pts.push(point)
13+
const key = `${point[0]},${point[1]}`
14+
this.ptsCnt[key] = (this.ptsCnt[key] || 0) + 1
15+
};
16+
17+
/**
18+
* @param {number[]} point
19+
* @return {number}
20+
*/
21+
DetectSquares.prototype.count = function(point) {
22+
let res = 0
23+
const [px, py] = point
24+
for(const [x, y] of this.pts) {
25+
if(px === x || py === y || Math.abs(px - x) !== Math.abs(py - y)) {
26+
continue
27+
}
28+
res += (this.ptsCnt[`${px},${y}`] || 0) * (this.ptsCnt[`${x},${py}`] || 0)
29+
}
30+
31+
return res
32+
};
33+
34+
/**
35+
* Your DetectSquares object will be instantiated and called as such:
36+
* var obj = new DetectSquares()
37+
* obj.add(point)
38+
* var param_2 = obj.count(point)
39+
*/
40+
41+
42+
// another
43+
244
var DetectSquares = function() {
345
this.xMap = new Map();
446
this.yMap = new Map();

0 commit comments

Comments
 (0)