关闭

Leetcode 391. Perfect Rectangle 完美矩形 解题报告

标签: leetcodeperfectrectangle解题java
3043人阅读 评论(0) 收藏 举报
分类:

1 解题思想

这道题是说给了一堆小矩形的坐标(左下角和右上角围成的),问其能否组合成一个完美的矩形(刚刚好,不多,不少,不交叉重复)。

这题肯定不能暴力开辟一个数组去做,或者其他类似思想。。

看了下Leetcode的讨论,最终总结出如下的做法:

 核心思想就是:能够正好围成一个矩形的情况就是:
有且只有:
- 最左下 最左上 最右下 最右上 的四个点只出现过一次,其他肯定是成对出现的(保证完全覆盖)
- 上面四个点围成的面积,正好等于所有子矩形的面积之和(保证不重复)

2 原题

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.

这里写图片描述

3 AC解

/**
 * 核心思想就是:能够正好围成一个矩形的情况就是:
 * 有且只有:
 *   - 最左下 最左上 最右下 最右上 的四个点只出现过一次,其他肯定是成对出现的(保证完全覆盖)
 *   - 上面四个点围成的面积,正好等于所有子矩形的面积之和(保证不重复)
 * Created by MebiuW on 16/8/29.
 */
public class Solution {
    public boolean isRectangleCover(int[][] rectangles) {
        int left = Integer.MAX_VALUE;
        int right = Integer.MIN_VALUE;
        int top = Integer.MIN_VALUE;
        int bottom = Integer.MAX_VALUE;
        int n = rectangles.length;
        HashSet<String> flags = new HashSet<String>();
        int totalArea = 0;
        for(int i=0;i<n;i++){
            left = Math.min(left,rectangles[i][0]);
            bottom = Math.min(bottom,rectangles[i][1]);
            right = Math.max(right,rectangles[i][2]);
            top = Math.max(top,rectangles[i][3]);
            totalArea += (rectangles[i][3]-rectangles[i][1])*(rectangles[i][2]-rectangles[i][0]);
            String pointLT = rectangles[i][0] + " "+ rectangles[i][3];
            String pointLB = rectangles[i][0] + " "+ rectangles[i][1];
            String pointRT = rectangles[i][2] + " "+ rectangles[i][3];
            String pointRB = rectangles[i][2] + " "+ rectangles[i][1];
            if (!flags.contains(pointLT)) flags.add(pointLT); else flags.remove(pointLT);
            if (!flags.contains(pointLB)) flags.add(pointLB); else flags.remove(pointLB);
            if (!flags.contains(pointRT)) flags.add(pointRT); else flags.remove(pointRT);
            if (!flags.contains(pointRB)) flags.add(pointRB); else flags.remove(pointRB);
        }
        if(flags.size()==4 && flags.contains(left+" "+top) && flags.contains(left+" "+bottom) && flags.contains(right+" "+bottom) && flags.contains(right+" "+top)){
            return totalArea == (right - left) * (top - bottom);
        }
        return false;
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:322876次
    • 积分:5840
    • 等级:
    • 排名:第4563名
    • 原创:270篇
    • 转载:0篇
    • 译文:0篇
    • 评论:61条
    最新评论