File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -15,3 +15,40 @@ const countCornerRectangles = function (grid) {
15
15
}
16
16
return ans
17
17
}
18
+
19
+ // another
20
+
21
+ // optimized
22
+
23
+ /**
24
+ * @param {number[][] } grid
25
+ * @return {number }
26
+ */
27
+ const countCornerRectangles = function ( grid ) {
28
+ let ans = 0
29
+ let largeLoopLen , smLoopLen , r
30
+ if ( grid . length > grid [ 0 ] . length ) {
31
+ r = false
32
+ largeLoopLen = grid . length
33
+ smLoopLen = grid [ 0 ] . length
34
+ } else {
35
+ r = true
36
+ largeLoopLen = grid [ 0 ] . length
37
+ smLoopLen = grid . length
38
+ }
39
+ for ( let i = 0 ; i < smLoopLen - 1 ; i ++ ) {
40
+ for ( let j = i + 1 ; j < smLoopLen ; j ++ ) {
41
+ let counter = 0
42
+ for ( let k = 0 ; k < largeLoopLen ; k ++ ) {
43
+ if ( r ) {
44
+ if ( grid [ i ] [ k ] === 1 && grid [ j ] [ k ] === 1 ) counter ++
45
+ } else {
46
+ if ( grid [ k ] [ i ] === 1 && grid [ k ] [ j ] === 1 ) counter ++
47
+ }
48
+
49
+ }
50
+ ans += ( counter * ( counter - 1 ) ) / 2
51
+ }
52
+ }
53
+ return ans
54
+ }
You can’t perform that action at this time.
0 commit comments