Skip to content

Commit 5108251

Browse files
committed
https://leetcode.cn/problems/minimum-area-rectangle-ii/
1 parent b8799c2 commit 5108251

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/minimum-area-rectangle-ii/
14+
1315
https://leetcode.cn/problems/minimum-area-rectangle
1416

1517
https://leetcode.cn/problems/bulb-switcher/

minimum-area-rectangle-ii/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default function minAreaFreeRect(points: number[][]): number {
2+
const ps = new Set(points.map((p) => JSON.stringify(p)));
3+
4+
let ans = Infinity;
5+
const n = points.length;
6+
for (let i = 0; i < n; i++) {
7+
for (let j = i + 1; j < n; j++) {
8+
for (let k = j + 1; k < n; ++k) {
9+
const p1 = points[i], p2 = points[j], p3 = points[k];
10+
const p4 = [p2[0] + p3[0] - p1[0], p2[1] + p3[1] - p1[1]];
11+
12+
if (
13+
0 ===
14+
(p2[0] - p1[0]) * (p3[0] - p1[0]) +
15+
(p2[1] - p1[1]) * (p3[1] - p1[1])
16+
) {
17+
const area = Math.hypot(p2[1] - p1[1], p2[0] - p1[0]) *
18+
Math.hypot(p3[1] - p1[1], p3[0] - p1[0]);
19+
if (area < ans && ps.has(JSON.stringify(p4))) {
20+
ans = area;
21+
}
22+
}
23+
}
24+
}
25+
}
26+
return Number.isFinite(ans) ? ans : 0;
27+
}

0 commit comments

Comments
 (0)