Skip to content

Commit f083ada

Browse files
authored
Create 939-minimum-area-rectangle.js
1 parent 1db2122 commit f083ada

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

939-minimum-area-rectangle.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
};

0 commit comments

Comments
 (0)