File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed
minimum-area-rectangle-ii Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ leetcode 测试
10
10
11
11
##### 包含的内容如下
12
12
13
+ https://leetcode.cn/problems/minimum-area-rectangle-ii/
14
+
13
15
https://leetcode.cn/problems/minimum-area-rectangle
14
16
15
17
https://leetcode.cn/problems/bulb-switcher/
Original file line number Diff line number Diff line change
1
+ export default function minAreaFreeRect ( points : number [ ] [ ] ) : number {
2
+ const ps = new Set ( points . map ( ( p ) => JSON . stringify ( p ) ) ) ;
3
+
4
+ let ans = Infinity ;
5
+ const n = points . length ;
6
+ for ( let i = 0 ; i < n ; i ++ ) {
7
+ for ( let j = i + 1 ; j < n ; j ++ ) {
8
+ for ( let k = j + 1 ; k < n ; ++ k ) {
9
+ const p1 = points [ i ] , p2 = points [ j ] , p3 = points [ k ] ;
10
+ const p4 = [ p2 [ 0 ] + p3 [ 0 ] - p1 [ 0 ] , p2 [ 1 ] + p3 [ 1 ] - p1 [ 1 ] ] ;
11
+
12
+ if (
13
+ 0 ===
14
+ ( p2 [ 0 ] - p1 [ 0 ] ) * ( p3 [ 0 ] - p1 [ 0 ] ) +
15
+ ( p2 [ 1 ] - p1 [ 1 ] ) * ( p3 [ 1 ] - p1 [ 1 ] )
16
+ ) {
17
+ const area = Math . hypot ( p2 [ 1 ] - p1 [ 1 ] , p2 [ 0 ] - p1 [ 0 ] ) *
18
+ Math . hypot ( p3 [ 1 ] - p1 [ 1 ] , p3 [ 0 ] - p1 [ 0 ] ) ;
19
+ if ( area < ans && ps . has ( JSON . stringify ( p4 ) ) ) {
20
+ ans = area ;
21
+ }
22
+ }
23
+ }
24
+ }
25
+ }
26
+ return Number . isFinite ( ans ) ? ans : 0 ;
27
+ }
You can’t perform that action at this time.
0 commit comments