GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #939 - Minimum Area Rectangle - LeetCode JavaScript Solutions

Back to all solutions

#939 - Minimum Area Rectangle

Medium • View on LeetCode • View on GitHub
Array Hash Table Math Sorting Geometry

Problem Description

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

/**
 * @param {number[][]} points
 * @return {number}
 */
var minAreaRect = function(points) {
  const pointSet = new Set(points.map(([x, y]) => `${x},${y}`));
  let result = 0;

  for (let i = 0; i < points.length; i++) {
    const [x1, y1] = points[i];
    for (let j = i + 1; j < points.length; j++) {
      const [x2, y2] = points[j];
      if (x1 !== x2 && y1 !== y2) {
        if (pointSet.has(`${x1},${y2}`) && pointSet.has(`${x2},${y1}`)) {
          const area = Math.abs(x1 - x2) * Math.abs(y1 - y2);
          result = result === 0 ? area : Math.min(result, area);
        }
      }
    }
  }

  return result;
};
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.