perfect-rectangle
https://leetcode.com/problems/perfect-rectangle/
// https://discuss.leetcode.com/topic/55944/o-n-log-n-sweep-line-solution
public class Solution {
public class Column implements Comparable<Column> {
int xs;
int[] rect;
public Column(int xs, int[] rect) {
this.xs = xs;
this.rect = rect;
}
public int compareTo(Column that) {
if (this.xs != that.xs) {
return this.xs - that.xs;
}
return this.rect[0] - that.rect[0];
}
}
public boolean isRectangleCover(int[][] rectangles) {
PriorityQueue<Column> pq = new PriorityQueue<Column>();
int[] border = {Integer.MAX_VALUE, Integer.MIN_VALUE};
for (int[] rect : rectangles) {
Column c1 = new Column(rect[0], rect);
Column c2 = new Column(rect[2], rect);
pq.add(c1);
pq.add(c2);
if (rect[1] < border[0]) {
border[0] = rect[1];
}
if (rect[3] > border[1]) {
border[1] = rect[3];
}
}
TreeSet<int[]> tset = new TreeSet<int[]> (new Comparator<int[]>(){
public int compare(int []rect1, int[]rect2) {
if (rect1[3] <= rect2[1]) {
return -1;
}
else if (rect1[1] >= rect2[3]) {
return 1;
}
else {
return 0;
}
}
});
int yRange = 0;
while (!pq.isEmpty()) {
int xs = pq.peek().xs;
while (!pq.isEmpty() && pq.peek().xs == xs) {
Column col = pq.poll();
int[] rect = col.rect;
if (xs == rect[2]) {
tset.remove(rect);
yRange -= rect[3] - rect[1];
}
else {
// xs == rect[0]
if (!tset.add(rect)) {
// intersect
return false;
}
yRange += rect[3] - rect[1];
}
}
// if pq.isEmpty(), the right line, no need to check
if (!pq.isEmpty() && yRange != border[1] - border[0]) {
return false;
}
}
return true;
}
}
perfect-rectangle的更多相关文章
- Leetcode: Perfect Rectangle
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [LeetCode] Perfect Rectangle 完美矩形
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [Swift]LeetCode391. 完美矩形 | Perfect Rectangle
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- 391. Perfect Rectangle
最后更新 一刷 16-Jan-2017 这个题我甚至不知道该怎么总结. 难就难在从这个题抽象出一种解法,看了别人的答案和思路= =然而没有归类总结到某种类型,这题相当于背了个题... 简单的说,除了最 ...
- 391 Perfect Rectangle 完美矩形
有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域.每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2]. ( ...
- Perfect Rectangle(完美矩形)
我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2] ...
- LeetCode赛题391----Perfect Rectangle
#391. Perfect Rectangle Given N axis-aligned rectangles where N > 0, determine if they all togeth ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- poj1979 Red And Black(DFS)
题目链接 http://poj.org/problem?id=1979 思路 floodfill问题,使用dfs解决 代码 #include <iostream> #include < ...
- ECSHOP中 {insert name='ads' id=$ads_id num=$ads_num}含义
<div class="smallban"> <ul> <!-- TemplateBeginEditabl ...
- UNP学习总结(一)
本文主要为对UNP第五章部分内容的实验和总结. UNP第五章对一个echo服务器和客户端在各种连接状态下的表现做了详细的分析,包括了: 正常启动和终止: accept返回前连接中止: 服务器进程终止: ...
- iOS WKWebview 网页开发适配指南
iOS WKWebview 网页开发适配指南 微信iOS客户端将于2017年3月1日前逐步升级为WKWebview内核,需要网页开发者提前做好网站的兼容检查和适配.如有问题,可参考文末联系方式,向我们 ...
- Django-高级特性
分页 1.固定显示分页数目 2.点击相应分页取出对应数据 具体实现: from django.utils.safestring import mark_safe class Pagination(ob ...
- 给HTML初学者的三十条最佳实践
Nettuts +运营最困难的方面是为很多技能水平不同的用户提供服务.如果我们发布太多高级教程,我的新手用户将无法从中受益.相反也是如此.我们尽我们最大的努力,但如果你觉得你被忽略了请联系我们.这个网 ...
- hdu 3879 方案选择
每日一水--- #include <cstdio> #include <cstring> #include <vector> #define oo 0x3f3f3f ...
- Android MediaCodec 的实例化方法
*由于作者水平限制,文中难免有错误和不恰当之处,望批评指正. *转载请注明出处:http://www.cnblogs.com/roger-yu/ MediaCodec的实例化方法主要有两种: 1.使用 ...
- Codeforces Round #357 (Div. 2) C. Heap Operations 模拟
C. Heap Operations 题目连接: http://www.codeforces.com/contest/681/problem/C Description Petya has recen ...
- Codeforces Round #234 (Div. 2) B. Inna and New Matrix of Candies SET的妙用
B. Inna and New Matrix of Candies time limit per test 1 second memory limit per test 256 megabytes i ...