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的更多相关文章

  1. Leetcode: Perfect Rectangle

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

  2. [LeetCode] Perfect Rectangle 完美矩形

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

  3. [Swift]LeetCode391. 完美矩形 | Perfect Rectangle

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

  4. 391. Perfect Rectangle

    最后更新 一刷 16-Jan-2017 这个题我甚至不知道该怎么总结. 难就难在从这个题抽象出一种解法,看了别人的答案和思路= =然而没有归类总结到某种类型,这题相当于背了个题... 简单的说,除了最 ...

  5. 391 Perfect Rectangle 完美矩形

    有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域.每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2]. ( ...

  6. Perfect Rectangle(完美矩形)

    我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域. 每个矩形用左下角的点和右上角的点的坐标来表示.例如, 一个单位正方形可以表示为 [1,1,2,2] ...

  7. LeetCode赛题391----Perfect Rectangle

    #391. Perfect Rectangle Given N axis-aligned rectangles where N > 0, determine if they all togeth ...

  8. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  10. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. 使用 AVA 做自动化测试

    http://colabug.com/710736.html

  2. thinkphp3.2路由美化,url简化

    thinkphp的路由功能很实用也很强大,可以简化url,有强大的正则匹配,可以做成任何想要的url样式. 在前台的config.php配置文件中: 1.首先开启路由 1 'URL_ROUTER_ON ...

  3. 【运维理论】RAID级别简介

    独立硬盘冗余阵列(RAID, Redundant Array of Independent Disks),旧称廉价磁盘冗余阵列(RAID, Redundant Array of Inexpensive ...

  4. windows svn 客户端连不上linux svn server

    采坑记录:linux服务器上svn://127.0.0.1可以正常使用,windows客户端远程连接不上,说明是端口号的问题. linux正常配置了iptables开启了3690端口,连接不上. 干脆 ...

  5. Codeforces.724G.Xor-matic Number of the Graph(线性基)

    题目链接 \(Description\) 给定一张带边权无向图.若存在u->v的一条路径使得经过边的边权异或和为s(边权计算多次),则称(u,v,s)为interesting triple(注意 ...

  6. Go语言特点

    作者:asta谢链接:https://www.zhihu.com/question/21409296/answer/18184584来源:知乎 1.Go有什么优势 可直接编译成机器码,不依赖其他库,g ...

  7. python开发_tkinter_多级子菜单

    在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...

  8. hdu 1150 Machine Schedule 最少点覆盖

    Machine Schedule Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  9. 封装libsvm成可程序调用的C/C++类

    libsvm很早之前就用了,现在封装一下方便自己使用,也方便大家更快的使用这个库,这个库一个挺有用的特性就是对测试样本的概率估计.源码在随笔的最后.liblinear的版本也是类似移植,主要是处理好数 ...

  10. C++使用autoreconf -vi出现error: possibly undefined macro: AC_LIBTOOL_WIN32_DLL If this token and others are legitimate, please use m4_pattern_allow. See the Autoconf documentation.

    安装这个:libtool  libsysfs yum install -y libtool libsysfs 参考: https://blog.csdn.net/yusiguyuan/article/ ...