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. mysql单表多timestamp报错#1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

    一个表中出现多个timestamp并设置其中一个为current_timestamp的时候经常会遇到#1293 - Incorrect table definition; there can be o ...

  2. 关于CSRF的那点事儿

    0x01 CSRF简介     CSRF,也称XSRF,即跨站请求伪造攻击,与XSS相似,但与XSS相比更难防范,是一种广泛存在于网站中的安全漏洞,经常与XSS一起配合攻击. 0x02 CSRF原理 ...

  3. String 的常用操作

    String 类,我可以不负责的说在 Java 中这个类应该是使用最频繁的类了.然而关于它的常用的操作,我却不甚了解.整理这个东西很反人性的,这些方法,你看到他的时候感觉很简单,但是真正用的时候还是不 ...

  4. python修改文件的属性

    1.执行attrib系统命令 ATTRIB [+R | -R] [+A | -A ] [+S | -S] [+H | -H] [+I | -I] [drive:][path][filename] [/ ...

  5. android 数据存储方式

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 1,文件 2,内容提供者 3,偏好设置 4,数据库 5,网络存储. 网络存储,就是上传到 ...

  6. 最大子段和问题Java实现

    最大子段和问题 一.问题描述 给定长度为n的整数序列,a[1...n], 求[1,n]某个子区间[i , j]使得a[i]+…+a[j]和最大. 例如(-2,11,-4,13,-5,2)的最大子段和为 ...

  7. [BZOJ3779]重组病毒(LCT+DFS序线段树)

    同[BZOJ4817]树点涂色,只是多了换根操作,分类讨论下即可. #include<cstdio> #include<algorithm> #define lc ch[x][ ...

  8. Alpha 冲刺报告5

    组长:吴晓晖 今天完成了哪些任务: 将服务端程序基本部署在阿里云上,还未进行测试 完成了手写记录的代码实现 处理团队问题 为明天的编程任务做准备 展示GitHub当日代码/文档签入记录: 明日计划: ...

  9. 20162303石亚鑫 第十二周hash补充博客

    要求 利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75) 关键字集合:85,75,57,60,65,(你的8位学号相加 ...

  10. malloc、calloc和realloc比较

    1.先看看它们的原型(stdlib.h): void *malloc( size_t size ); void *calloc( size_t numElements, size_t sizeOfEl ...