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;
    }
}

原文地址:https://www.cnblogs.com/lizzyluvcoding/p/10792499.html

时间: 2024-10-14 07:57:37

Sweep Line的相关文章

[fb 面经] intervals sweep line

Leetcode 上的题 Merge Intervals Insert Intervals Meeting Rooms Meeting RoomsII Topcoder tutorial: Sweep Line Algorithms some from gitbook about sweep line problems on leetcode: https://robinliu.gitbooks.io/leetcode/content/Sweep_Line.html 面经: 题1: Given

[算法]Comparison of the different algorithms for Polygon Boolean operations

Comparison of the different algorithms for  Polygon Boolean operations. Michael Leonov 1998 http://www.angusj.com/delphi/clipper.php#screenshots http://www.complex-a5.ru/polyboolean/comp.html http://www.angusj.com/delphi/clipper.php#screenshots Intro

Leetcode: Find Right Interval

Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i. For any interval i, you ne

多边形三角网剖分资料(转)

仔细查了一下资料.关于多边形三角网剖分,已经有人在网上做了归纳总结. OpenGL的 glutesselation虽然好用,但是据说算法效率不行. 比较好的算法还是Ploy2Tri算法.有时间还是得试一试. Triangulation of Simple PolygonsBen Discoe, notes from 2001.02.11, updated through 2009.01 I needed some code for tessellating polygons, which cou

刷题方法论

[转自一亩三分田]谈谈面试官在面试coding题目时的考察终点与心理活动 本人简介: 曾经微软dev, 35+, 10年经验, 有FLG offer.  去年加入一个start up 公司, 最近前景不明, 在犹豫要不要去个稳定点的大公司.  我从sde开始面试其他人, 到现在估计面试过100+人次的面试和debrief. 我面过coding, problem solving, design, behavior.  本帖子只谈论纯粹coding, 视情况讨论要不要再开帖子讨论其他方面. 本文涉及

CS3K.com 九章算法强化班

Advanced Data Structure -- Union Find Number of Islands 题目 思路I:BFS 避免相同位置的元素重复入列,访问过的元素都要标记为已访问.BFS为了得到下一个点的坐标,所以需要建立一个表示位置的坐标类. 思路II:并查集 等价于求集合中连通块的个数. Number of Islands II 题目 思路:并查集,把二维数组转化为一维father数组. LeetCode 547. Friend Circles 题目 思路:并查集的典型应用.题目

#11:假日将尽的挣扎——6

cf818A 1 #include <bits/stdc++.h> 2 #define ll __int64 3 using namespace std; 4 5 ll n, k; 6 7 int main() { 8 cin >> n >> k; 9 ll a = n / 2 / (k+1); 10 ll b = k * a; 11 printf("%I64d %I64d %I64d\n", a, b, n-a-b); 12 return 0; 1

R:incomplete final line found by readTableHeader on

报错: In read.table("abc.txt", header = T) :  incomplete final line found by readTableHeader on 'abc.txt' 解决方法: 在数据文件abc.txt最后一行加上回车即可解决.

windows git安装后更换the line ending conversions

Is there a file or menu that will let me change the settings on how to deal with line endings? There are 3 options: Checkout Windows-style, commit Unix-style line endings Git will convert LF to CRLF when checking out text files. When committing text