File tree 1 file changed +53
-0
lines changed
1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1
1
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
+
2
55
const DetectSquares = function ( ) {
3
56
this . pts = [ ]
4
57
this . ptsCnt = { }
You can’t perform that action at this time.
0 commit comments