Skip to content

Commit b8799c2

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

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-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
14+
1315
https://leetcode.cn/problems/bulb-switcher/
1416

1517
https://leetcode.cn/problems/concatenated-words/

minimum-area-rectangle/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export default function minAreaRect(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+
if (
9+
points[i][0] !== points[j][0] &&
10+
points[i][1] !== points[j][1]
11+
) {
12+
const area = Math.abs(points[j][0] - points[i][0]) *
13+
Math.abs(points[j][1] - points[i][1]);
14+
if (area > ans) continue;
15+
if (
16+
ps.has(JSON.stringify([points[i][0], points[j][1]])) &&
17+
ps.has(JSON.stringify([points[j][0], points[i][1]]))
18+
) {
19+
ans = Math.min(
20+
ans,
21+
area,
22+
);
23+
}
24+
}
25+
}
26+
}
27+
return Number.isFinite(ans) ? ans : 0;
28+
}

0 commit comments

Comments
 (0)