File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 748
748
936|[ Stamping The Sequence] ( ./solutions/0936-stamping-the-sequence.js ) |Hard|
749
749
937|[ Reorder Data in Log Files] ( ./solutions/0937-reorder-data-in-log-files.js ) |Medium|
750
750
938|[ Range Sum of BST] ( ./solutions/0938-range-sum-of-bst.js ) |Easy|
751
+ 939|[ Minimum Area Rectangle] ( ./solutions/0939-minimum-area-rectangle.js ) |Medium|
751
752
966|[ Vowel Spellchecker] ( ./solutions/0966-vowel-spellchecker.js ) |Medium|
752
753
970|[ Powerful Integers] ( ./solutions/0970-powerful-integers.js ) |Easy|
753
754
976|[ Largest Perimeter Triangle] ( ./solutions/0976-largest-perimeter-triangle.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 939. Minimum Area Rectangle
3
+ * https://leetcode.com/problems/minimum-area-rectangle/
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 a rectangle formed from these points, with sides parallel to the
9
+ * X and Y axes. If there is not any such rectangle, return 0.
10
+ */
11
+
12
+ /**
13
+ * @param {number[][] } points
14
+ * @return {number }
15
+ */
16
+ var minAreaRect = function ( points ) {
17
+ const pointSet = new Set ( points . map ( ( [ x , y ] ) => `${ x } ,${ y } ` ) ) ;
18
+ let result = 0 ;
19
+
20
+ for ( let i = 0 ; i < points . length ; i ++ ) {
21
+ const [ x1 , y1 ] = points [ i ] ;
22
+ for ( let j = i + 1 ; j < points . length ; j ++ ) {
23
+ const [ x2 , y2 ] = points [ j ] ;
24
+ if ( x1 !== x2 && y1 !== y2 ) {
25
+ if ( pointSet . has ( `${ x1 } ,${ y2 } ` ) && pointSet . has ( `${ x2 } ,${ y1 } ` ) ) {
26
+ const area = Math . abs ( x1 - x2 ) * Math . abs ( y1 - y2 ) ;
27
+ result = result === 0 ? area : Math . min ( result , area ) ;
28
+ }
29
+ }
30
+ }
31
+ }
32
+
33
+ return result ;
34
+ } ;
You can’t perform that action at this time.
0 commit comments