Skip to content

Latest commit

 

History

History
81 lines (66 loc) · 2.9 KB

File metadata and controls

81 lines (66 loc) · 2.9 KB

963. Minimum Area Rectangle II

You are given an array of points in the X-Y plane points where points[i] = [xi, yi].

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 10-5 of the actual answer will be accepted.

Example 1:

Input: points = [[1,2],[2,1],[1,0],[0,1]]
Output: 2.00000
Explanation: The minimum area rectangle occurs at [1,2],[2,1],[1,0],[0,1], with an area of 2.

Example 2:

Input: points = [[0,1],[2,1],[1,1],[1,0],[2,0]]
Output: 1.00000
Explanation: The minimum area rectangle occurs at [1,0],[1,1],[2,1],[2,0], with an area of 1.

Example 3:

Input: points = [[0,3],[1,2],[3,1],[1,3],[2,1]]
Output: 0
Explanation: There is no possible rectangle to form from these points.

Constraints:

  • 1 <= points.length <= 50
  • points[i].length == 2
  • 0 <= xi, yi <= 4 * 104
  • All the given points are unique.

Solutions (Rust)

1. Solution

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
    }
}