• [leetcode]253. Meeting Rooms II 会议室II


    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.

    Example 1:

    Input: [[0, 30],[5, 10],[15, 20]]
    Output: 2

    Example 2:

    Input: [[7,10],[2,4]]
    Output: 1

    思路

    1. Sort the intervals by starting time so it will always find the earliest available meeting once the most recent one is end.

    2. keep track of the endings showing when there is a meeting room gets available.


     代码

    public int minMeetingRooms(int[][] intervals) {
            if (intervals == null || intervals.length == 0) return 0; // corner case
        
            Arrays.sort(intervals, (int[]a, int[]b)->(a[0] - b[0]));  //掌握自定义sort的写法
    PriorityQueue<Integer> minHeap = new PriorityQueue<>((a,b)-> a - b); minHeap.add(intervals[0][1]); for(int i =1; i<intervals.length;i++) { //尤其注意是初始是 i=1 if(intervals[i][0] >= minHeap.peek()) { minHeap.poll(); } minHeap.add(intervals[i][1]); } return minHeap.size(); }

      

  • 相关阅读:
    构建之法阅读笔记01
    学习进度13
    学习进度12
    个人冲刺十
    个人冲刺九
    个人冲刺八
    学习进度11
    个人冲刺七
    个人冲刺六
    [HDU 1232 ]畅通工程
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/9808255.html
Copyright © 2020-2023  润新知