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.

Refer to https://discuss.leetcode.com/topic/56052/really-easy-understanding-solution-o-n-java

and   https://discuss.leetcode.com/topic/55923/o-n-solution-by-counting-corners-with-detailed-explaination

Idea

Consider how the corners of all rectangles appear in the large rectangle if there's a perfect rectangular cover.
Rule1: The local shape of the corner has to follow one of the three following patterns

    • Corner of the large rectangle (blue): it occurs only once among all rectangles
    • T-junctions (green): it occurs twice among all rectangles
    • Cross (red): it occurs four times among all rectangles

For each point being a corner of any rectangle, it should appear even times except the 4 corners of the large rectangle. So we can put those points into a hash map and remove them if they appear one more time.

At the end, we should only get 4 points.

Rule2:  the large rectangle area should be equal to the sum of small rectangles

 public class Solution {
public boolean isRectangleCover(int[][] rectangles) {
if (rectangles==null || rectangles.length==0 || rectangles[0].length==0) return false;
int subrecAreaSum = 0; //sum of subrectangle's area
int x1 = Integer.MAX_VALUE; //large rectangle bottom left x-axis
int y1 = Integer.MAX_VALUE; //large rectangle bottom left y-axis
int x2 = Integer.MIN_VALUE; //large rectangle top right x-axis
int y2 = Integer.MIN_VALUE; //large rectangle top right y-axis HashSet<String> set = new HashSet<String>(); // store points for(int[] rec : rectangles) {
//check if it has large rectangle's 4 points
x1 = Math.min(x1, rec[0]);
y1 = Math.min(y1, rec[1]);
x2 = Math.max(x2, rec[2]);
y2 = Math.max(y2, rec[3]); //calculate sum of subrectangles
subrecAreaSum += (rec[2]-rec[0]) * (rec[3] - rec[1]); //store this rectangle's 4 points into hashSet
String p1 = Integer.toString(rec[0]) + "" + Integer.toString(rec[1]);
String p2 = Integer.toString(rec[0]) + "" + Integer.toString(rec[3]);
String p3 = Integer.toString(rec[2]) + "" + Integer.toString(rec[1]);
String p4 = Integer.toString(rec[2]) + "" + Integer.toString(rec[3]); if (!set.add(p1)) set.remove(p1);
if (!set.add(p2)) set.remove(p2);
if (!set.add(p3)) set.remove(p3);
if (!set.add(p4)) set.remove(p4);
} if (set.size()!=4 || !set.contains(x1+""+y1) || !set.contains(x1+""+y2) || !set.contains(x2+""+y1) || !set.contains(x2+""+y2))
return false;
return subrecAreaSum == (x2-x1) * (y2-y1);
}
}

Leetcode: Perfect Rectangle的更多相关文章

  1. [LeetCode] Perfect Rectangle 完美矩形

    Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...

  2. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...

  3. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  4. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  5. [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  6. [LeetCode] Perfect Squares 完全平方数

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  7. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  8. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  9. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

随机推荐

  1. 实现LoadRunner多个场景的顺序执行(命令行)

    应用场景:假设有3个不同的测试场景,分别为并发登录.核心业务.可靠性测试,3个场景有先后执行顺序.由于白天测试机器另有用处,只能在晚上进行性能测试,这时我们的期望是能否把测试场景都设定好之后晚上自动运 ...

  2. linux面试题及答案

    http://www.cnblogs.com/itech/archive/2011/02/12/1952857.html 一.填空题:1. 在Linux系统中,以 文件 方式访问设备 .2. Linu ...

  3. laravel md5+salt 密码

    laravel 默认用的登录密码加密方式是: $password = Hash::make('password'); 修改密码加密方式为: $password = md5('password'.'sa ...

  4. 后台list 如何转换为json格式

    request.setCharacterEncoding("utf-8"); response.setCharacterEncoding( "UTF-8"); ...

  5. 数据库主键跟外键+修改mysql的密码

    update myspl.user set password=PASSWORD(设置的密码)  where user='root'; 如果修改错误:先执行use mysple;再重复上面的代码. 一. ...

  6. 【译】Android系统架构

    让我们来快速预览一下整个android系统的架构.从下面的图中我们可以发现,这个架构分为几个不同的层,底层为上一层提供服务.  Linux Kernel android系统建立在一个坚固的基石上:Li ...

  7. insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a=10001

    insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a ...

  8. yum安装node.js

    1.安装EPEL库 yum install epel-release 2.安装Node.js yum install nodejs 3.安装nodejs中常用的npm软件包管理器 yum instal ...

  9. tableView中自定header视图的重用问题

    在UItableView中使用代理方发 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger ...

  10. CodeTimer

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...