Meeting Rooms II

 1 /**
 2  * Definition for an interval.
 3  * public class Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() { start = 0; end = 0; }
 7  *     Interval(int s, int e) { start = s; end = e; }
 8  * }
 9  */
10 public class Solution {
11     public int minMeetingRooms(Interval[] intervals) {
12         if (intervals.length < 2) {
13             return intervals.length;
14         }
15
16         Arrays.sort(intervals, new Comparator<Interval>() {
17             @Override
18             public int compare(Interval a, Interval b) {
19                 return a.start - b.start;
20             }
21         });
22
23         PriorityQueue<Interval> queue = new PriorityQueue<>(intervals.length, new Comparator<Interval>() {
24            @Override
25            public int compare(Interval a, Interval b) {
26                return a.end - b.end;
27            }
28         });
29
30         queue.offer(intervals[0]);
31         for (int i = 1; i < intervals.length; i++) {
32             Interval current = queue.poll();
33             if (intervals[i].start >= current.end) {
34                 current.end = intervals[i].end;
35             } else {
36                 queue.offer(intervals[i]);
37             }
38             queue.offer(current);
39         }
40         return queue.size();
41     }
42 }
时间: 2024-10-13 06:24:30

Meeting Rooms II的相关文章

[LeetCode] Meeting Rooms 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. For example,Given [[0, 30],[5, 10],[15, 20]],return 2. 这道题是之前那道Meeting Rooms的拓展,那道题只让我们是

253 Meeting Rooms 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. For example, Given [[0, 30],[5, 10],[15, 20]], return 2. 看到区间求重叠的部分 , 就想到对区间排序(start, en

[LeetCode#253] Meeting Rooms II

Problem: 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. For example,Given [[0, 30],[5, 10],[15, 20]],return 2. Analysis: This problem l

Meeting Rooms II -- LeetCode

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. For example,Given [[0, 30],[5, 10],[15, 20]],return 2. 思路:贪心. 将所有interval按照开始时间从早到晚排序.之后

leetcode253 - Meeting Rooms II - medium

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: 2Example 2:Input: [[7,10],[2,4]]Output

[LeetCode] Meeting Rooms I &amp; II

Meeting Rooms Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example,Given [[0, 30],[5, 10],[15, 20]],return false. 1 /** 2 * Definition

LeetCode-Meeting Rooms 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. For example, Given [[0, 30],[5, 10],[15, 20]], return 2. Analysis: It is the same with L

LeetCode 252. Meeting Rooms (会议室)

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example,Given [[0, 30],[5, 10],[15, 20]],return false. 题目标签:sort 这道题目给了我们一个array的会议时间,让我们

Leetcode Meeting Rooms

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example,Given [[0, 30],[5, 10],[15, 20]],return false. 解题思路: 先对start sort, O(nlgn), 然后比较S