Skip to content

Commit d938188

Browse files
committed
Add solution #939
1 parent 0aba783 commit d938188

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@
748748
936|[Stamping The Sequence](./solutions/0936-stamping-the-sequence.js)|Hard|
749749
937|[Reorder Data in Log Files](./solutions/0937-reorder-data-in-log-files.js)|Medium|
750750
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|
751752
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
752753
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
753754
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
};

0 commit comments

Comments
 (0)