File tree 2 files changed +52
-1
lines changed
2 files changed +52
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,047 LeetCode solutions in JavaScript
1
+ # 1,048 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcode.com/ ] ( https://leetcode.com/ )
4
4
771
771
959|[ Regions Cut By Slashes] ( ./solutions/0959-regions-cut-by-slashes.js ) |Medium|
772
772
960|[ Delete Columns to Make Sorted III] ( ./solutions/0960-delete-columns-to-make-sorted-iii.js ) |Hard|
773
773
962|[ Maximum Width Ramp] ( ./solutions/0962-maximum-width-ramp.js ) |Medium|
774
+ 963|[ Minimum Area Rectangle II] ( ./solutions/0963-minimum-area-rectangle-ii.js ) |Medium|
774
775
966|[ Vowel Spellchecker] ( ./solutions/0966-vowel-spellchecker.js ) |Medium|
775
776
970|[ Powerful Integers] ( ./solutions/0970-powerful-integers.js ) |Easy|
776
777
976|[ Largest Perimeter Triangle] ( ./solutions/0976-largest-perimeter-triangle.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 963. Minimum Area Rectangle II
3
+ * https://leetcode.com/problems/minimum-area-rectangle-ii/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given an array of points in the X-Y plane points where points[i] = [xi, yi].
7
+ *
8
+ * Return the minimum area of any rectangle formed from these points, with sides not necessarily
9
+ * parallel to the X and Y axes. If there is not any such rectangle, return 0.
10
+ *
11
+ * Answers within 10^-5 of the actual answer will be accepted.
12
+ */
13
+
14
+ /**
15
+ * @param {number[][] } points
16
+ * @return {number }
17
+ */
18
+ var minAreaFreeRect = function ( points ) {
19
+ const n = points . length ;
20
+ const set = new Set ( points . map ( p => `${ p [ 0 ] } ,${ p [ 1 ] } ` ) ) ;
21
+ let result = 0 ;
22
+
23
+ for ( let i = 0 ; i < n ; i ++ ) {
24
+ for ( let j = i + 1 ; j < n ; j ++ ) {
25
+ const [ x1 , y1 ] = points [ i ] ;
26
+ const [ x2 , y2 ] = points [ j ] ;
27
+ const dx1 = x2 - x1 ;
28
+ const dy1 = y2 - y1 ;
29
+
30
+ for ( let k = 0 ; k < n ; k ++ ) {
31
+ if ( k === i || k === j ) continue ;
32
+ const [ x3 , y3 ] = points [ k ] ;
33
+ const dx2 = x3 - x1 ;
34
+ const dy2 = y3 - y1 ;
35
+
36
+ if ( dx1 * dx2 + dy1 * dy2 !== 0 ) continue ;
37
+
38
+ const x4 = x2 + dx2 ;
39
+ const y4 = y2 + dy2 ;
40
+
41
+ if ( set . has ( `${ x4 } ,${ y4 } ` ) ) {
42
+ const area = Math . sqrt ( dx1 * dx1 + dy1 * dy1 ) * Math . sqrt ( dx2 * dx2 + dy2 * dy2 ) ;
43
+ result = result === 0 ? area : Math . min ( result , area ) ;
44
+ }
45
+ }
46
+ }
47
+ }
48
+
49
+ return result ;
50
+ } ;
You can’t perform that action at this time.
0 commit comments