Sweep Line
391. Number of Airplanes in the Sky
https://www.lintcode.com/problem/number-of-airplanes-in-the-sky/description?_from=ladder&&fromId=4
思路:将起飞时间和降落时间放到同一个数组中, 标识出是起飞还是降落时间, 然后对数组排序,遍历数组即可, 碰到起飞计数器加一, 碰到降落计数器减一. 维护最大值作为答案.
注意降落优先于起飞 可通过flag标记降落为0 上升为1,保证按time相等按flag排序降落为先
/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/ class Point{
int time;
int flag; Point(int t, int s) {
this.time = t;
this.flag = s;
}
public static Comparator<Point> PointComparator = new Comparator<Point>() {
public int compare(Point p1, Point p2) {
if(p1.time == p2.time)
return p1.flag - p2.flag;
else
return p1.time - p2.time;
}
};
} public class Solution {
/**
* @param airplanes: An interval array
* @return: Count of airplanes are in the sky.
*/
public int countOfAirplanes(List<Interval> airplanes) {
// write your code here
List<Point> list = new ArrayList<>(airplanes.size()*2);
for(Interval i:airplanes){
list.add(new Point(i.start,1));
list.add(new Point(i.end,-1));
} Collections.sort(list,Point.PointComparator);
int count=0,ans =1;
for(Point p:list){
if(p.flag==1)
count++;
else
count--; ans = Math.max(ans,count);
} return ans; }
}
919. Meeting Rooms II
https://www.lintcode.com/problem/meeting-rooms-ii/description?_from=ladder&&fromId=4
/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/
class Meeting{
int time;
int flag;
public Meeting(int time,int flag){
this.time = time;
this.flag = flag;
} public static Comparator<Meeting> comparator = new Comparator<Meeting>(){
@Override
public int compare(Meeting m1, Meeting m2){
if(m1.time==m2.time){
return m1.flag - m2.flag;
}
return m1.time-m2.time;
}
};
}
public class Solution {
/**
* @param intervals: an array of meeting time intervals
* @return: the minimum number of conference rooms required
*/
public int minMeetingRooms(List<Interval> intervals) {
// Write your code here
if(intervals == null || intervals.size()==0){
return 0;
} List<Meeting> meetings = new ArrayList<>();
for(Interval i:intervals){
meetings.add(new Meeting(i.start,1));
meetings.add(new Meeting(i.end,-1));
} Collections.sort(meetings,Meeting.comparator); int count =0;
int ans =0;
for(Meeting m:meetings){
if(m.flag==1){
count++;
} if(m.flag==-1){
count--;
} ans = Math.max(ans,count);
} return ans;
}
}
821. Time Intersection
https://www.lintcode.com/problem/time-intersection/description?_from=ladder&&fromId=4
/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/
class Point{
int time;
int flag; Point(int t, int s) {
this.time = t;
this.flag = s;
}
public static Comparator<Point> PointComparator = new Comparator<Point>() {
public int compare(Point p1, Point p2) {
if(p1.time == p2.time)
return p1.flag - p2.flag;
else
return p1.time - p2.time;
}
};
} public class Solution {
/**
* @param seqA: the list of intervals
* @param seqB: the list of intervals
* @return: the time periods
*/
public List<Interval> timeIntersection(List<Interval> seqA, List<Interval> seqB) {
// Write your code here
List<Interval> res = new ArrayList<>();
if(seqA==null || seqB==null){
return res;
} List<Point> list = new ArrayList<>();
for(Interval i:seqA){
list.add(new Point(i.start,1));
list.add(new Point(i.end,-1));
} for(Interval i:seqB){
list.add(new Point(i.start,1));
list.add(new Point(i.end,-1));
} Collections.sort(list,Point.PointComparator); int start =-1;
int count=0;
for(Point p:list){
if(p.flag==1)
count++;
else
count--; if(count==2){
start = p.time;
} if(count==1 && start!=-1){
res.add(new Interval(start,p.time));
start = -1;
}
} return res;
}
}
131. The Skyline Problem
https://www.lintcode.com/problem/the-skyline-problem/description?_from=ladder&&fromId=4
class Pair implements Comparable{
int index; //坐标
int status; //是up还是down
int height; //高度
Pair(int index,int status,int height){
this.index = index;
this.status = status;
this.height = height;
}
//先按坐标,再按status,再按高度
public int compareTo(Object o){
Pair p = (Pair)o;
if(this.index==p.index){
if(this.status==p.status){
return this.height - p.height;
}else{
return this.status - p.status;
}
}else{
return this.index- p.index;
}
}
}
public class Solution {
/**
* @param buildings: A list of lists of integers
* @return: Find the outline of those buildings
*/
private static int UP = 0;
private static int DOWN = 1;
public List<List<Integer>> buildingOutline(int[][] buildings) {
// write your code here
List<List<Integer>> res = new ArrayList<>();
if(buildings ==null || buildings.length==0||buildings[0].length==0){
return res;
}
List<Pair> pairs = new ArrayList<>();
for(int i=0;i< buildings.length;i++){
pairs.add(new Pair(buildings[i][0],UP,buildings[i][2]));
pairs.add(new Pair(buildings[i][1],DOWN,buildings[i][2]));
}
Collections.sort(pairs);
TreeMap<Integer,Integer> heightMap = new TreeMap<>();
int preIndex =0;
int preHeight =0;
for(Pair pair:pairs){
if(!heightMap.containsKey(pair.height)){
heightMap.put(pair.height,1);
}else{
if(pair.status==UP){
heightMap.put(pair.height,heightMap.get(pair.height)+1);
}else{
heightMap.put(pair.height,heightMap.get(pair.height)-1);
if(heightMap.get(pair.height)==0){
heightMap.remove(pair.height);
}
}
}
int currHeight = heightMap.size()==0?0:heightMap.lastKey();
if(preHeight!=currHeight){
if(preHeight!=0 && preIndex!=pair.index){
res.add(eachSkyLine(preIndex,pair.index,preHeight));
}
preHeight = currHeight;
preIndex = pair.index;
}
}
return res;
}
private List<Integer> eachSkyLine(int start,int end,int height){
List<Integer> list = new ArrayList<>();
list.add(start);
list.add(end);
list.add(height);
return list;
}
}
Sweep Line的更多相关文章
- sweep line-The Skyline Problem
2020-01-10 17:51:05 问题描述: 问题求解: 本题是经典的sweep line问题. 对于sweep line问题我们需要考虑的只有两点: 1. 延水平方向 / 时间方向 :时间队列 ...
- Leetcode: Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- [算法]Comparison of the different algorithms for Polygon Boolean operations
Comparison of the different algorithms for Polygon Boolean operations. Michael Leonov 1998 http://w ...
- 218. The Skyline Problem *HARD* -- 矩形重叠
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- Poly2Tri介绍[转]
https://blog.csdn.net/xys206006/article/details/83002326 这是Poly2Tri介绍的翻译博文.原文链接:http://sites-final.u ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- Voronoi Diagram——维诺图
Voronoi图定义 任意两点p 和q 之间的欧氏距离,记作 dist(p, q) .就平面情况而言,我们有 dist(p, q) = (px-qx)2+ (py-qy)2 ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- BUAA软件工程_结对编程
1.写在前面 项目 内容 所属课程 2020春季计算机学院软件工程(罗杰 任健) (北航) 作业要求 结对项目作业 课程目标 培养软件开发能力 本作业对实现目标的具体作用 培养结对编程开发项目的能力 ...
随机推荐
- PHP 微信公众号开发 - 消息推送
项目微信公众号开发,需要做用户消息推送,记录下来以便日后使用 1,接上一篇文章,可以查看如何获取用户openid PHP 微信公众号开发 - 获取用户信息 2,添加模板消息 3,查看模板详情 根据模板 ...
- FZU 1977 Pandora adventure (DP)
题意:给定一个图,X表示不能走,O表示必须要走,*表示可走可不走,问你多少种走的法,使得形成一个回路. 析: 代码如下: #pragma comment(linker, "/STACK:10 ...
- Spring 之 AOP
面向方面的编程,即 AOP,是一种编程技术,它允许程序员对横切关注点或横切典型的职责分界线的行为(例如日志和事务管理)进行模块化.AOP 的核心构造是方面, 它将那些影响多个类的行为封装到可重用的模块 ...
- [label][JavaScript]读nowmagic - js词法作用域、调用对象与闭包
原文链接: http://www.nowamagic.net/librarys/veda/detail/1305 作用域(scope) JavaScript 中的函数 ...
- 作业 c++编写
1.第一版本程序Prog1:+ 给定一个数组,实现数组元素求和:,具体要求:实现对一维数组(a[100])的所有元素相加运算.+ 数据准备:a)数组长度:100:b)数组数据来源:实验数据A列:1~1 ...
- Hibernate实体类注解的问题
刚刚和八千哥弄一个问题,这个很诡异的问题,困扰了我这么长时间.哎,说来惭愧. 用三大框架写毕设,结果今天获取前台数的时候,发现传值有个传不到. 我一开始用的是名为cows的数据,后来换了个数据库,加了 ...
- MSP430 G2553 比较器Comparator_A+、数据流程图DFD、状态转换图STD
一.CA+构造 MSP430G2553带有一个比较器Comparator_A+(CA+),其构造框图如下图所示. 二.输入 & 输出 如上图所示,比较器有一个同向输入端(V+)和一个反向输入端 ...
- C#中索引器的作用和实现。
官方描述:索引器允许类或结构的实例就像数组一样进行索引.索引器形态类似于,不同之处在于它们的取值函数采用参数. 这一功能在创建集合类的场合特别有用,而在其他某些情况下,比如处理大型文件或者抽象有些资源 ...
- C# 依赖注入 & MEF
之前面试有问道依赖注入,因为一直是做客户端的发开发,没有接触这个,后边工作接触到了MEF,顺便熟悉一下依赖注入 详细的概念解释就不讲了,网上一大把,个人觉着依赖注入本质是为了解耦,方便扩展 依赖注入的 ...
- c#设计模式之观察者模式(Observer Pattern)
场景出发 一个月高风黑的晚上,突然传来了尖锐的猫叫,宁静被彻底打破,狗开始吠了,大人醒了,婴儿哭了,小偷跑了 这个过程,如果用面向对象语言来描述,简单莫过于下: public class Cat { ...