File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } points
3
+ * @return {number }
4
+ */
5
+ const minAreaRect = function ( points ) {
6
+ const xmap = { } , ymap = { }
7
+ points . forEach ( e => {
8
+ const [ x , y ] = e
9
+ if ( ! xmap . hasOwnProperty ( x ) ) xmap [ x ] = new Set ( )
10
+ if ( ! ymap . hasOwnProperty ( y ) ) ymap [ y ] = new Set ( )
11
+ xmap [ x ] . add ( y )
12
+ ymap [ y ] . add ( x )
13
+ } )
14
+ let res = Infinity
15
+ for ( let i = 0 , len = points . length ; i < len - 1 ; i ++ ) {
16
+ const [ x , y ] = points [ i ]
17
+ for ( let j = i + 1 ; j < len ; j ++ ) {
18
+ const [ x1 , y1 ] = points [ j ]
19
+ if ( x === x1 || y === y1 ) continue
20
+ let area = Infinity
21
+ if ( xmap [ x ] . has ( y1 ) && ymap [ y ] . has ( x1 ) ) area = Math . abs ( x - x1 ) * Math . abs ( y - y1 )
22
+ else continue
23
+ res = Math . min ( res , area )
24
+ }
25
+ }
26
+ return res === Infinity ? 0 : res
27
+ } ;
You can’t perform that action at this time.
0 commit comments