Skip to content

Commit

Permalink
+ problem 963
Browse files Browse the repository at this point in the history
  • Loading branch information
fruit-in committed Sep 28, 2024
1 parent e64c609 commit 7b3dfdd
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 0 deletions.
81 changes: 81 additions & 0 deletions Problemset/0963-Minimum Area Rectangle II/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 963. Minimum Area Rectangle II
You are given an array of points in the **X-Y** plane `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.

Return *the minimum area of any rectangle formed from these points, with sides **not necessarily parallel** to the X and Y axes*. If there is not any such rectangle, return `0`.

Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.

#### Example 1:
![](https://assets.leetcode.com/uploads/2018/12/21/1a.png)
<pre>
<strong>Input:</strong> points = [[1,2],[2,1],[1,0],[0,1]]
<strong>Output:</strong> 2.00000
<strong>Explanation:</strong> The minimum area rectangle occurs at [1,2],[2,1],[1,0],[0,1], with an area of 2.
</pre>

#### Example 2:
![](https://assets.leetcode.com/uploads/2018/12/22/2.png)
<pre>
<strong>Input:</strong> points = [[0,1],[2,1],[1,1],[1,0],[2,0]]
<strong>Output:</strong> 1.00000
<strong>Explanation:</strong> The minimum area rectangle occurs at [1,0],[1,1],[2,1],[2,0], with an area of 1.
</pre>

#### Example 3:
![](https://assets.leetcode.com/uploads/2018/12/22/3.png)
<pre>
<strong>Input:</strong> points = [[0,3],[1,2],[3,1],[1,3],[2,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no possible rectangle to form from these points.
</pre>

#### Constraints:
* `1 <= points.length <= 50`
* `points[i].length == 2`
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 4 * 10<sup>4</sup></code>
* All the given points are **unique**.

## Solutions (Rust)

### 1. Solution
```Rust
use std::collections::HashSet;

impl Solution {
pub fn min_area_free_rect(points: Vec<Vec<i32>>) -> f64 {
let points_set = points.iter().map(|p| (p[0], p[1])).collect::<HashSet<_>>();
let mut ret = f64::NAN;

for i in 0..points.len() {
let (xi, yi) = (points[i][0], points[i][1]);

for j in i + 1..points.len() {
let (xj, yj) = (points[j][0], points[j][1]);
let ij2 = (xi - xj).pow(2) as f64 + (yi - yj).pow(2) as f64;

for k in j + 1..points.len() {
let (xk, yk) = (points[k][0], points[k][1]);
let ik2 = (xi - xk).pow(2) as f64 + (yi - yk).pow(2) as f64;
let jk2 = (xj - xk).pow(2) as f64 + (yj - yk).pow(2) as f64;

if ij2 + ik2 == jk2 && points_set.contains(&(xj + xk - xi, yj + yk - yi)) {
ret = ret.min(ij2.sqrt() * ik2.sqrt());
} else if ij2 + jk2 == ik2 && points_set.contains(&(xi + xk - xj, yi + yk - yj))
{
ret = ret.min(ij2.sqrt() * jk2.sqrt());
} else if ik2 + jk2 == ij2 && points_set.contains(&(xi + xj - xk, yi + yj - yk))
{
ret = ret.min(ik2.sqrt() * jk2.sqrt());
}
}
}
}

if ret.is_nan() {
return 0.;
}

ret
}
}
```
88 changes: 88 additions & 0 deletions Problemset/0963-Minimum Area Rectangle II/README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# 963. 最小面积矩形 II
给定在 xy 平面上的一组点,确定由这些点组成的任何矩形的最小面积,其中矩形的边**不一定平行于** x 轴和 y 轴。

如果没有任何矩形,就返回 0。

#### 示例 1:
![](https://assets.leetcode.com/uploads/2018/12/21/1a.png)
<pre>
<strong>输入:</strong> points = [[1,2],[2,1],[1,0],[0,1]]
<strong>输出:</strong> 2.00000
<strong>解释:</strong> 最小面积的矩形出现在 [1,2],[2,1],[1,0],[0,1] 处,面积为 2。
</pre>

#### 示例 2:
![](https://assets.leetcode.com/uploads/2018/12/22/2.png)
<pre>
<strong>输入:</strong> points = [[0,1],[2,1],[1,1],[1,0],[2,0]]
<strong>输出:</strong> 1.00000
<strong>解释:</strong> 最小面积的矩形出现在 [1,0],[1,1],[2,1],[2,0] 处,面积为 1。
</pre>

#### 示例 3:
![](https://assets.leetcode.com/uploads/2018/12/22/3.png)
<pre>
<strong>输入:</strong> points = [[0,3],[1,2],[3,1],[1,3],[2,1]]
<strong>输出:</strong> 0
<strong>解释:</strong> 没法从这些点中组成任何矩形。
</pre>

#### 示例 4:
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/21/4c.png)
<pre>
<strong>输入:</strong> [[3,1],[1,1],[0,1],[2,1],[3,3],[3,2],[0,2],[2,3]]
<strong>输出:</strong> 2.00000
<strong>解释:</strong> 最小面积的矩形出现在 [2,1],[2,3],[3,3],[3,1] 处,面积为 2。
</pre>

#### 提示:
1. `1 <= points.length <= 50`
2. `0 <= points[i][0] <= 40000`
3. `0 <= points[i][1] <= 40000`
4. 所有的点都是不同的。
5. 与真实值误差不超过 `10^-5` 的答案将视为正确结果。

## 题解 (Rust)

### 1. 题解
```Rust
use std::collections::HashSet;

impl Solution {
pub fn min_area_free_rect(points: Vec<Vec<i32>>) -> f64 {
let points_set = points.iter().map(|p| (p[0], p[1])).collect::<HashSet<_>>();
let mut ret = f64::NAN;

for i in 0..points.len() {
let (xi, yi) = (points[i][0], points[i][1]);

for j in i + 1..points.len() {
let (xj, yj) = (points[j][0], points[j][1]);
let ij2 = (xi - xj).pow(2) as f64 + (yi - yj).pow(2) as f64;

for k in j + 1..points.len() {
let (xk, yk) = (points[k][0], points[k][1]);
let ik2 = (xi - xk).pow(2) as f64 + (yi - yk).pow(2) as f64;
let jk2 = (xj - xk).pow(2) as f64 + (yj - yk).pow(2) as f64;

if ij2 + ik2 == jk2 && points_set.contains(&(xj + xk - xi, yj + yk - yi)) {
ret = ret.min(ij2.sqrt() * ik2.sqrt());
} else if ij2 + jk2 == ik2 && points_set.contains(&(xi + xk - xj, yi + yk - yj))
{
ret = ret.min(ij2.sqrt() * jk2.sqrt());
} else if ik2 + jk2 == ij2 && points_set.contains(&(xi + xj - xk, yi + yj - yk))
{
ret = ret.min(ik2.sqrt() * jk2.sqrt());
}
}
}
}

if ret.is_nan() {
return 0.;
}

ret
}
}
```
39 changes: 39 additions & 0 deletions Problemset/0963-Minimum Area Rectangle II/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::collections::HashSet;

impl Solution {
pub fn min_area_free_rect(points: Vec<Vec<i32>>) -> f64 {
let points_set = points.iter().map(|p| (p[0], p[1])).collect::<HashSet<_>>();
let mut ret = f64::NAN;

for i in 0..points.len() {
let (xi, yi) = (points[i][0], points[i][1]);

for j in i + 1..points.len() {
let (xj, yj) = (points[j][0], points[j][1]);
let ij2 = (xi - xj).pow(2) as f64 + (yi - yj).pow(2) as f64;

for k in j + 1..points.len() {
let (xk, yk) = (points[k][0], points[k][1]);
let ik2 = (xi - xk).pow(2) as f64 + (yi - yk).pow(2) as f64;
let jk2 = (xj - xk).pow(2) as f64 + (yj - yk).pow(2) as f64;

if ij2 + ik2 == jk2 && points_set.contains(&(xj + xk - xi, yj + yk - yi)) {
ret = ret.min(ij2.sqrt() * ik2.sqrt());
} else if ij2 + jk2 == ik2 && points_set.contains(&(xi + xk - xj, yi + yk - yj))
{
ret = ret.min(ij2.sqrt() * jk2.sqrt());
} else if ik2 + jk2 == ij2 && points_set.contains(&(xi + xj - xk, yi + yj - yk))
{
ret = ret.min(ik2.sqrt() * jk2.sqrt());
}
}
}
}

if ret.is_nan() {
return 0.;
}

ret
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@
[958][958l] |[Check Completeness of a Binary Tree][958] |![py]
[961][961l] |[N-Repeated Element in Size 2N Array][961] |![rs]
[962][962l] |[Maximum Width Ramp][962] |![rs]
[963][963l] |[Minimum Area Rectangle II][963] |![rs]
[965][965l] |[Univalued Binary Tree][965] |![py]
[966][966l] |[Vowel Spellchecker][966] |![py]
[967][967l] |[Numbers With Same Consecutive Differences][967] |![rb]&nbsp;&nbsp;![rs]
Expand Down Expand Up @@ -2116,6 +2117,7 @@
[958]:Problemset/0958-Check%20Completeness%20of%20a%20Binary%20Tree/README.md#958-check-completeness-of-a-binary-tree
[961]:Problemset/0961-N-Repeated%20Element%20in%20Size%202N%20Array/README.md#961-n-repeated-element-in-size-2n-array
[962]:Problemset/0962-Maximum%20Width%20Ramp/README.md#962-maximum-width-ramp
[963]:Problemset/0963-Minimum%20Area%20Rectangle%20II/README.md#963-minimum-area-rectangle-ii
[965]:Problemset/0965-Univalued%20Binary%20Tree/README.md#965-univalued-binary-tree
[966]:Problemset/0966-Vowel%20Spellchecker/README.md#966-vowel-spellchecker
[967]:Problemset/0967-Numbers%20With%20Same%20Consecutive%20Differences/README.md#967-numbers-with-same-consecutive-differences
Expand Down Expand Up @@ -3612,6 +3614,7 @@
[958l]:https://leetcode.com/problems/check-completeness-of-a-binary-tree/
[961l]:https://leetcode.com/problems/n-repeated-element-in-size-2n-array/
[962l]:https://leetcode.com/problems/maximum-width-ramp/
[963l]:https://leetcode.com/problems/minimum-area-rectangle-ii/
[965l]:https://leetcode.com/problems/univalued-binary-tree/
[966l]:https://leetcode.com/problems/vowel-spellchecker/
[967l]:https://leetcode.com/problems/numbers-with-same-consecutive-differences/
Expand Down
3 changes: 3 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@
[958][958l] |[二叉树的完全性检验][958] |![py]
[961][961l] |[重复 N 次的元素][961] |![rs]
[962][962l] |[最大宽度坡][962] |![rs]
[963][963l] |[最小面积矩形 II][963] |![rs]
[965][965l] |[单值二叉树][965] |![py]
[966][966l] |[元音拼写检查器][966] |![py]
[967][967l] |[连续差相同的数字][967] |![rb]&nbsp;&nbsp;![rs]
Expand Down Expand Up @@ -2116,6 +2117,7 @@
[958]:Problemset/0958-Check%20Completeness%20of%20a%20Binary%20Tree/README_CN.md#958-二叉树的完全性检验
[961]:Problemset/0961-N-Repeated%20Element%20in%20Size%202N%20Array/README_CN.md#961-重复-n-次的元素
[962]:Problemset/0962-Maximum%20Width%20Ramp/README_CN.md#962-最大宽度坡
[963]:Problemset/0963-Minimum%20Area%20Rectangle%20II/README_CN.md#963-最小面积矩形-ii
[965]:Problemset/0965-Univalued%20Binary%20Tree/README_CN.md#965-单值二叉树
[966]:Problemset/0966-Vowel%20Spellchecker/README_CN.md#966-元音拼写检查器
[967]:Problemset/0967-Numbers%20With%20Same%20Consecutive%20Differences/README_CN.md#967-连续差相同的数字
Expand Down Expand Up @@ -3612,6 +3614,7 @@
[958l]:https://leetcode.cn/problems/check-completeness-of-a-binary-tree/
[961l]:https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/
[962l]:https://leetcode.cn/problems/maximum-width-ramp/
[963l]:https://leetcode.cn/problems/minimum-area-rectangle-ii/
[965l]:https://leetcode.cn/problems/univalued-binary-tree/
[966l]:https://leetcode.cn/problems/vowel-spellchecker/
[967l]:https://leetcode.cn/problems/numbers-with-same-consecutive-differences/
Expand Down

0 comments on commit 7b3dfdd

Please sign in to comment.