Skip to main content
Back to problems
#939
Medium Algorithms

Minimum area rectangle

Array Hash Table Math Geometry Sorting
55.3% acceptance
Feb 25, 2026
2099
298
You are given an array of points in the X-Y plane points where points[i] = [xi, yi]. Return the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes. If there is not any such rectangle, return 0.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn min_area_rect(points: Vec<Vec<i32>>) -> i32 {
    use std::collections::HashSet;
    let set: HashSet<(i32,i32)> = points.iter().map(|p| (p[0], p[1])).collect();
    let n = points.len();
    let mut min_area = i32::MAX;
    for i in 0..n {
      for j in i+1..n {
        let (x1, y1) = (points[i][0], points[i][1]);
        let (x2, y2) = (points[j][0], points[j][1]);
        if x1 != x2 && y1 != y2 {
          if set.contains(&(x1, y2)) && set.contains(&(x2, y1)) {
            let area = (x2-x1).abs() * (y2-y1).abs();
            min_area = min_area.min(area);
          }
        }
      }
    }
    if min_area == i32::MAX { 0 } else { min_area }
  }
}