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

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

Note:

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

核心思想是利用对角线的原理定下两个点,根据对角线在找到另外两个点

如何把一个array pair存到hash里,第一次我用了int -> string, 超时了

class Solution {
    public int minAreaRect(int[][] points) {
        if(points == null || points.length == 0 || points[0].length ==0){
            return -1;
        }
        Set<String> set = new HashSet<>();
        for(int[] point : points){
            set.add(Integer.toString(point[0])+":"+Integer.toString(point[1]));
        }
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < points.length-1; i++){
            for(int j = i+1; j < points.length; j++){
                int [] pointA = points[i];
                int [] pointB = points[j];
                if(pointA[0] != pointB[0] && pointA[1] != pointB[1]){
                    if(set.contains(Integer.toString(pointA[0])+":"+Integer.toString(pointB[1])) && set.contains(Integer.toString(pointB[0])+":"+Integer.toString(pointA[1]))){
                        int cur = Math.abs((pointB[0] - pointA[0]) * (pointB[1] - pointA[1]));
                        if(cur < min){
                            min = cur;
                        }
                    }
                }
            }
        }
        if(min == Integer.MAX_VALUE){
            return 0;
        }
        return min;
    }
}

第二次看了别人的代码, 原来可以按照把每一个pair的x存成key,y轴存成一个hashset。

class Solution {
    public int minAreaRect(int[][] points) {
        if(points == null || points.length == 0 || points[0].length ==0){
            return 0;
        }
        Map<Integer, Set<Integer>> map = new HashMap<>();
        for(int[] point : points){
            if(map.containsKey(point[0])){
                map.get(point[0]).add(point[1]);
            }
            else{
                Set<Integer> set = new HashSet<Integer>();
                set.add(point[1]);
                map.put(point[0], set);
            }
        }

        int min = Integer.MAX_VALUE;
        for(int i = 0; i < points.length-1; i++){
            for(int j = i+1; j < points.length; j++){
                int [] pointA = points[i];
                int [] pointB = points[j];
                if(pointA[0] != pointB[0] && pointA[1] != pointB[1]){
                    if(map.get(pointA[0]).contains(pointB[1]) && map.get(pointB[0]).contains(pointA[1])){
                        int cur = Math.abs((pointB[0] - pointA[0]) * (pointB[1] - pointA[1]));
                        if(cur < min){
                            min = cur;
                        }
                    }
                }
            }
        }
        if(min == Integer.MAX_VALUE){
            return 0;
        }
        return min;
    }
}

原文地址:https://www.cnblogs.com/incrediblechangshuo/p/9968546.html

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

LeetCode - Minimum Area Rectangle的相关文章

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

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

[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】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 OJ] Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

[leetcode]85. Maximal Rectangle 最大矩形

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. Example: Input: [ ["1","0","1","0","0"], ["1","0","1",&qu

LeetCode: Minimum Window Substring [076]

[题目] Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such

LeetCode &quot;Minimum Path Sum&quot; - 2D DP

An intuitive 2D DP: dp[i][j] = min(grid[i-1][j-1] + dp[i-1][j], grid[i-1][j-1] + dp[i][j+1]) class Solution { public: int minPathSum(vector<vector<int> > &grid) { // dp[i][j] = min(dp[i-1][j] + dp[i][j], dp[i][j-1] + dp[i][j]); int n = gri