Leetcode: Perfect Rectangle
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的更多相关文章
- [LeetCode] Perfect Rectangle 完美矩形
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- leetcode Maximal Rectangle 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- [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 ...
- [LeetCode] Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...
随机推荐
- apache磁盘缓存配置
确保mod_cache和mod_disk_cache是开启的 配置如下: CacheDefaultExpire 86400 #失效时间,单位秒CacheEnable disk / #缓存路径 ...
- js之操作JSON数据
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...
- reduce()
Professional.JavaScript.for.Web.Developers.3rd.Edition.Jan.2012 var value = [1,2,3,4,5]; var sum = v ...
- ArrayList调用remove方法需要注意的地方
ArrayList中有remove 方法和 removeAll方法, ArrayList中不仅继承了接口Collection中的remove方法,而且还扩展了remove方法. Collection中 ...
- java面试问道的
1.java可重入锁 2.Hashmap原理.说说hashMap是怎样实现的(这个之前看过,顺利回答上.还回答了多线程的问题出现的原因,面试官表示很惊讶的样.用hashmap实现hashset 3.
- freebsd 禁用root登录ssh并给普通用户登录权限
转自http://www.linux521.com/2009/system/200904/2021.html http://www.myhack58.com/Article/48/67/2011/30 ...
- java并发编程-线程池的使用
参考文章:http://www.cnblogs.com/dolphin0520/p/3932921.html 深入剖析线程池实现原理 将从下面几个方面讲解: 1.线程池状态 2.任务的执行 3.线程池 ...
- SqlServer数据组织结构
page页 每个页面8KB,连续的8个页面称之为一个区extents, 如:2.18MB的一个DB的区大约有 2.18 MB (2,293,760 字节)=2,293,760b/8kb=280个页面= ...
- EF扩展库(批量操作)
EF删除和修改数据只能先从数据库取出,然后再进行删除 delete from Table1 where Id>5; update Table1 set Age=10; 我们需要这样操作 //删除 ...
- linux 开机启动设置
操作系统:Ubuntu12.04硬件环境:HP CQ45 当用户使用sudo apt-get install安装完apache和mysql之后,这些服务默认是开机启动的,但是有的时候需要 ...