Skip to content

Commit 14d50ac

Browse files
authored
Update 939-minimum-area-rectangle.js
1 parent f083ada commit 14d50ac

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

939-minimum-area-rectangle.js

+23
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,26 @@ const minAreaRect = function(points) {
2525
}
2626
return res === Infinity ? 0 : res
2727
};
28+
29+
// another
30+
31+
/**
32+
* @param {number[][]} points
33+
* @return {number}
34+
*/
35+
const minAreaRect = function (points) {
36+
let ans = Infinity
37+
const isPoint = {}
38+
points.forEach(([x, y]) => (isPoint[x * 40000 + y] = true))
39+
for (let idx1 = 0; idx1 < points.length - 1; idx1++) {
40+
const [x1, y1] = points[idx1]
41+
for (let idx2 = idx1 + 1; idx2 < points.length; idx2++) {
42+
const [x2, y2] = points[idx2]
43+
const area = Math.abs((x1 - x2) * (y1 - y2))
44+
if (area === 0 || area >= ans) continue
45+
if (isPoint[x1 * 40000 + y2] && isPoint[x2 * 40000 + y1]) ans = area
46+
}
47+
}
48+
return ans !== Infinity ? ans : 0
49+
}
50+

0 commit comments

Comments
 (0)