Skip to content

Commit 5668d6b

Browse files
authored
Update 2013-detect-squares.js
1 parent 5741d55 commit 5668d6b

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

2013-detect-squares.js

+53
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,57 @@
11

2+
var DetectSquares = function() {
3+
this.mat = Array.from({length: 1001}, () => Array.from({length: 1001}, () => 0));
4+
this.ya = new Map()
5+
};
6+
7+
/**
8+
* @param {number[]} point
9+
* @return {void}
10+
*/
11+
DetectSquares.prototype.add = function(point) {
12+
const [x, y] = point;
13+
this.mat[x][y]++;
14+
if(this.ya.get(y) == null) this.ya.set(y, new Map())
15+
if(this.ya.get(y).get(x) == null) this.ya.get(y).set(x, 0)
16+
this.ya.get(y).set(x, this.ya.get(y).get(x) + 1)
17+
};
18+
19+
/**
20+
* @param {number[]} point
21+
* @return {number}
22+
*/
23+
DetectSquares.prototype.count = function(point) {
24+
const [x, y] = point;
25+
let res = 0
26+
if(!this.ya.has(y)) return res
27+
for(const [key, cnt] of this.ya.get(y)) {
28+
const i = +key
29+
const d = Math.abs(x - i)
30+
if(d === 0) continue
31+
let j = y + d
32+
if(j >= 0 && j < 1001) {
33+
res += cnt * this.mat[x][j] * this.mat[i][j]
34+
}
35+
j = y - d
36+
if(j >= 0 && j < 1001) {
37+
res += cnt * this.mat[x][j] * this.mat[i][j]
38+
}
39+
}
40+
41+
return res
42+
};
43+
44+
/**
45+
* Your DetectSquares object will be instantiated and called as such:
46+
* var obj = new DetectSquares()
47+
* obj.add(point)
48+
* var param_2 = obj.count(point)
49+
*/
50+
51+
// another
52+
53+
54+
255
const DetectSquares = function() {
356
this.pts = []
457
this.ptsCnt = {}

0 commit comments

Comments
 (0)