资讯专栏INFORMATION COLUMN

leetcode391. Perfect Rectangle

不知名网友 / 269人阅读

摘要:题目要求用一个二维数组来表示一堆矩形,二维数组中的每一行分别记录矩形左下角和右上角的坐标。该理想情况下矩形的面积应当等于所有矩形的面积之和。一旦不相等,则一定无法构成大的矩形。其次,光判断面积并不足够,可以这样三个矩形构成的图形。

题目要求
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region.

Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2]. (coordinate of bottom-left point is (1, 1) and top-right point is (2, 2)).

Example 1:

rectangles = [
  [1,1,3,3],
  [3,1,4,2],
  [3,2,4,4],
  [1,3,2,4],
  [2,3,3,4]
]

Return true. All 5 rectangles together form an exact cover of a rectangular region.

Example 2:

rectangles = [
  [1,1,2,3],
  [1,3,2,4],
  [3,1,4,2],
  [3,2,4,4]
]

Return false. Because there is a gap between the two rectangular regions.

Example 3:

rectangles = [
  [1,1,3,3],
  [3,1,4,2],
  [1,3,2,4],
  [3,2,4,4]
]

Return false. Because there is a gap in the top center.

Example 4:

rectangles = [
  [1,1,3,3],
  [3,1,4,2],
  [1,3,2,4],
  [2,2,4,4]
]

Return false. Because two of the rectangles overlap with each other.

用一个二维数组来表示一堆矩形,二维数组中的每一行分别记录矩形左下角和右上角的坐标。试判断这些矩形拼接成的新的图形是否还是一个矩形。如果矩形存在重合,则不构成矩形,见图例4.

思路和代码

这是一道纯粹的考验思维的一道题目。
首先我们知道,这些矩形如果能够拼接成一个大的矩形,那么大的矩形的左下角坐标一定是所有矩形中最小的x1和y1值构成的,同理,右上角坐标一定是由最大的x2和y2的值构成的。该理想情况下矩形的面积应当等于所有矩形的面积之和。一旦不相等,则一定无法构成大的矩形。

其次,光判断面积并不足够,可以这样三个矩形构成的图形[1,1,2,2],[2,2,3,3],[2,1,3,3]。可以看到该图形的理想矩形就是一个2*2的正方形,它的面积与所有的小矩形的和相等,但是这些小矩形并没有构成该理想的矩形。那么我们能用什么方式来过滤掉这种矩形呢。只能从矩形的顶点入手了。
我们知道,任何一个能够构成理想矩形的小矩形,一定会有顶点的重合,直到只剩下四个重合度为1的点,即大矩形的四个顶点。其它的所有顶点都应当有另一个矩形与其重合。因此我们只需要留下所有度为1的顶点,判断其是否都是大矩形的四个顶点即可。

代码如下:

public boolean isRectangleCover(int[][] rectangles) {
        if(rectangles==null || rectangles.length == 0 || rectangles[0].length == 0) return false;
        int areaSum = 0;
        int x1 = Integer.MAX_VALUE;
        int x2 = Integer.MIN_VALUE;
        int y1 = Integer.MAX_VALUE;
        int y2 = Integer.MIN_VALUE;
        
        Set points = new HashSet<>(rectangles.length * 4);
        for(int[] rectangle : rectangles) {
            x1 = Math.min(rectangle[0], x1);
            x2 = Math.max(rectangle[2], x2);
            y1 = Math.min(rectangle[1], y1);
            y2 = Math.max(rectangle[3], y2);
            
            areaSum += (rectangle[0] - rectangle[2]) * (rectangle[1] - rectangle[3]);
            String s1 = rectangle[0] + " " + rectangle[1];
            String s2 = rectangle[0] + " " + rectangle[3];
            String s3 = rectangle[2] + " " + rectangle[1];
            String s4 = rectangle[2] + " " + rectangle[3];
            if (!points.add(s1)) {
                points.remove(s1);
            }
            if (!points.add(s2)) {
                points.remove(s2);
            }
            if (!points.add(s3)) {
                points.remove(s3);
            }
            if (!points.add(s4)) {
                points.remove(s4);
            }
        }
        if(!points.contains(x1 + " " + y1) || 
                !points.contains(x1 + " " + y2) ||
                !points.contains(x2 + " " + y1) ||
                !points.contains(x2 + " " + y2) ||
                points.size() != 4) return false;
        return areaSum == (x2 - x1) * (y2 - y1);
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73628.html

相关文章

  • Perfect Rectangle

    摘要:首先确定上下的边界,左右线段按照横坐标排序。检查填充满上图的情况就组成不了一个长方形。找重合和有空隙只需要把所有横坐标在的线段排序之后检查首位相连,且起点,终点。且最后成的面积等于小矩形的面积和。 Perfect Rectangle 题目链接:https://leetcode.com/problems... 扫描线,哪个方向都行。我是从左往右扫,矩阵按照左右的边来存。showImg(h...

    SolomonXie 评论0 收藏0
  • [LC] Perfect Rectangle / Find the Difference / Eli

    Find the Difference User Accepted: 812User Tried: 861Total Accepted: 1362Total Submissions: 1552Difficulty: Easy Given two strings s and t which consist of only lowercase letters. String t is generate...

    mingde 评论0 收藏0
  • [Leetcode] Perfect Squares 完美平方数

    摘要:动态规划复杂度时间空间思路如果一个数可以表示为一个任意数加上一个平方数,也就是,那么能组成这个数最少的平方数个数,就是能组成最少的平方数个数加上因为已经是平方数了。 Perfect Squares Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4...

    Moxmi 评论0 收藏0
  • [LeetCode] 367. Valid Perfect Square

    Problem Given a positive integer num, write a function which returns True if num is a perfect square else False. Example For example:Given num = 16Returns True Solution class Solution { public boo...

    sean 评论0 收藏0
  • [LeetCode] 279. Perfect Squares

    Problem Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. Example 1: Input: n = 12Output: 3 Explanation: 12 = 4 + 4 + 4.Exampl...

    mist14 评论0 收藏0

发表评论

0条评论

不知名网友

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<