LeetCode 963. Minimum Area Rectangle II

原题链接在这里:https://leetcode.com/problems/minimum-area-rectangle-ii/

题目:

Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these points, with sides not necessarily parallel to the x and y axes.

If there isn‘t any rectangle, return 0.

Example 1:

Input: [[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: [[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: [[0,3],[1,2],[3,1],[1,3],[2,1]]
Output: 0
Explanation: There is no possible rectangle to form from these points.

Example 4:

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

Note:

  1. 1 <= points.length <= 50
  2. 0 <= points[i][0] <= 40000
  3. 0 <= points[i][1] <= 40000
  4. All points are distinct.
  5. Answers within 10^-5 of the actual value will be accepted as correct.

题解:

For rectangle, diagonal length are equal and diagonal equally cut each other.

For a pair of nodes, we store distance, and middle x and middle y as key.

If there is antoehr pair of nodes having same distance, middle x and middle y, then these 2 pairs could be used to construct rectangle.

Time Complexity: O(n^2). n = points.length.

Space: O(n^2).

AC java:

 1 class Solution {
 2     public double minAreaFreeRect(int[][] points) {
 3         if(points == null || points.length < 4){
 4             return 0;
 5         }
 6
 7         int n = points.length;
 8         HashMap<String, List<int[]>> hm = new HashMap<>();
 9         for(int i = 0; i < n; i++){
10             for(int j = i + 1; j < n; j++){
11                 double diaDist = dist(points[i], points[j]);
12                 double midX = ((double)points[i][0] + (double)points[j][0]) / 2.0;
13                 double midY = ((double)points[i][1] + (double)points[j][1]) / 2.0;
14                 String key = midX + "," + midY + "," + diaDist;
15                 hm.putIfAbsent(key, new ArrayList<>());
16                 hm.get(key).add(new int[]{i, j});
17             }
18         }
19
20         double res = Double.MAX_VALUE;
21         for(List<int[]> value : hm.values()){
22             if(value.size() > 1){
23                 for(int i = 0; i < value.size(); i++){
24                     for(int j = i + 1; j < value.size(); j++){
25                         int [] p1 = points[value.get(i)[0]];
26                         int [] p2 = points[value.get(j)[0]];
27                         int [] p3 = points[value.get(j)[1]];
28                         res = Math.min(res, dist(p1, p2) * dist(p1, p3));
29                     }
30                 }
31             }
32         }
33
34         return res == Double.MAX_VALUE ? 0 : res;
35     }
36
37     private double dist(int [] p1, int [] p2){
38         long x = p1[0] - p2[0];
39         long y = p1[1] - p2[1];
40         return Math.sqrt(x * x + y * y);
41     }
42 }

类似Minimum Area Rectangle.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12114048.html

时间: 2024-08-30 17:57:41

LeetCode 963. Minimum Area Rectangle II的相关文章

LC 963. Minimum Area Rectangle II

Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these points, with sides not necessarily parallel to the x and y axes. If there isn't any rectangle, return 0. Example 1: Input: [[1,2],[2,1],[1,0],[0,1]]

LeetCode 939. Minimum Area Rectangle

原题链接在这里:https://leetcode.com/problems/minimum-area-rectangle/ 题目: Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes. If there isn't any rectangle, return

[Swift Weekly Contest 116]LeetCode963. 最小面积矩形 II | Minimum Area Rectangle II

Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these points, with sides not necessarily parallel to the x and y axes. If there isn't any rectangle, return 0. Example 1: Input: [[1,2],[2,1],[1,0],[0,1]]

LeetCode - Minimum Area Rectangle

Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes. If there isn't any rectangle, return 0. Example 1: Input: [[1,1],[1,3],[3,1],[3,3],[2,2]] Output: 4 Ex

【leetcode】939. Minimum Area Rectangle

题目如下: Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes. If there isn't any rectangle, return 0. Example 1: Input: [[1,1],[1,3],[3,1],[3,3],[2,2]] Output

leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 r

LeetCode:492. Construct the Rectangle

1 package Today; 2 //LeetCode:492. Construct the Rectangle 3 /* 4 For a web developer, it is very important to know how to design a web page's size. 5 So, given a specific rectangular web page's area, your job by now is to design a rectangular web pa

Java [Leetcode 119]Pascal&#39;s Triangle II

题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. 解题思路: 每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数. 代码描述: public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> r

LeetCode --- 64. Minimum Path Sum

题目链接:Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 这道题的要求是在m*n