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 0.

Example 1:

Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4

Example 2:

Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2

Note:

  1. 1 <= points.length <= 500
  2. 0 <= points[i][0] <= 40000
  3. 0 <= points[i][1] <= 40000
  4. All points are distinct.

题解:

Use a HashMap to maintain all y of the same x.

Then try to find the diagonal nodes, get one node from x and one node from y, if x[0] == y[0] || x[1] == y[1], skip, they can‘t be diagonal nodes.

Else if in x[0] HashMap entry contains y[1] and in y[0] HashMap entry contains x[1], then x and y could diagonal nodes. Use it to update rectangle size.

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

Space: O(n).

AC Java:

 1 class Solution {
 2     public int minAreaRect(int[][] points) {
 3         if(points == null || points.length < 4){
 4             return 0;
 5         }
 6
 7         HashMap<Integer, Set<Integer>> hm = new HashMap<>();
 8         for(int [] p : points){
 9             hm.putIfAbsent(p[0], new HashSet<>());
10             hm.get(p[0]).add(p[1]);
11         }
12
13         int res = Integer.MAX_VALUE;
14         for(int [] p1 : points){
15             for(int [] p2 : points){
16                 if(p1[0] == p2[0] || p1[1] == p2[1]){
17                     continue;
18                 }
19
20                 if(hm.get(p1[0]).contains(p2[1]) && hm.get(p2[0]).contains(p1[1])){
21                     res = Math.min(res, Math.abs(p2[0] - p1[0]) * Math.abs(p2[1] - p1[1]));
22                 }
23             }
24         }
25
26         return res == Integer.MAX_VALUE ? 0 : res;
27     }
28 }

跟上Minimum Area Rectangle II.

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

时间: 2024-08-30 14:01:02

LeetCode 939. Minimum Area Rectangle的相关文章

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

【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

[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]]

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

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

【Leetcode】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. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

【leetcode】Minimum Window Substring

问题: 给定两个字符串,S,T,返回S中包含T中所有字符的最短的字串,若不存在,则返回"".时间复杂度为O(n). 例如:S = "ADOBCODEBANC" T = "ABC" 返回BANC 生活场景: 把问题具体化现实化一点.有n层楼,每层楼里放有一个物品,现在老板给你一个物品清单,里面是要你集齐的物品,你可以乘坐电梯,但是电梯只停一次,停在哪一层,就从哪一层开始向楼上搜集物品,至于要在那层停电梯,由你自己选择. 这里我们当然选择即能集齐物品