meeting room I & II

本文介绍了一种用于判断是否能够参加所有会议的算法,并提供两种实现方式。一种是通过记录每个时间点的状态变化来判断是否存在冲突;另一种是直接对会议区间进行排序后检查相邻区间是否重叠。此外还给出了一种求最小会议室数量的方法。

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 public class Solution {
 2     public boolean canAttendMeetings(Interval[] intervals) {
 3         List<TimePoint> list = new ArrayList<TimePoint>();
 4         
 5         for (Interval interval : intervals) {
 6            list.add(new TimePoint(interval.start, true));
 7            list.add(new TimePoint(interval.end, false));
 8         }
 9         
10         Collections.sort(list, new Comparator<TimePoint>() {
11             public int compare(TimePoint t1, TimePoint t2) {
12                 if (t1.time < t2.time) {
13                     return -1;
14                 } else if (t1.time > t2.time) {
15                     return 1;
16                 } else {
17                     if (t1.isStart) {
18                         return 1;
19                     } else {
20                         return -1;
21                     }
22                 }  
23             }
24         });
25         int count = 0;
26         for (TimePoint t : list) {
27             if (t.isStart) {
28                 count++;
29                 if (count == 2) return false;
30             } else {
31                 count--;
32             }
33         }
34         return true;
35     }
36 }
37 
38 class TimePoint {
39     int time;
40     boolean isStart;
41     
42     public TimePoint(int time, boolean isStart) {
43         this.time = time;
44         this.isStart = isStart;
45     }
46 }

网上看到一个更简单的方法:先sort intervals, 然后看俩个相邻的点是否有重合。

 1 public boolean canAttendMeetings(Interval[] intervals) {
 2     Arrays.sort(intervals, new Comparator<Interval>(){
 3         public int compare(Interval a, Interval b){
 4             return a.start-b.start;
 5         }
 6     });
 7  
 8     for(int i=0; i<intervals.length-1; i++){
 9         if(intervals[i].end>intervals[i+1].start){
10             return false;
11         }
12     }
13     return true;
14 }

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.

方法和第一种相似。

 1 public class Solution {
 2     public int minMeetingRooms(Interval[] intervals) {
 3         List<TimePoint> list = new ArrayList<TimePoint>();
 4         
 5         for (Interval interval : intervals) {
 6            list.add(new TimePoint(interval.start, true));
 7            list.add(new TimePoint(interval.end, false));
 8         }
 9         
10         Collections.sort(list, new Comparator<TimePoint>() {
11             public int compare(TimePoint t1, TimePoint t2) {
12                 if (t1.time < t2.time) {
13                     return -1;
14                 } else if (t1.time > t2.time) {
15                     return 1;
16                 } else {
17                     if (t1.isStart) {
18                         return 1;
19                     } else {
20                         return -1;
21                     }
22                 }  
23             }
24         });
25         int count = 0;
26         int max = 0;
27         for (TimePoint t : list) {
28             if (t.isStart) {
29                 count++;
30                 max = Math.max(count, max);
31             } else {
32                 count--;
33             }
34         }
35         return max;
36     }
37 }
38 
39 class TimePoint {
40     int time;
41     boolean isStart;
42     
43     public TimePoint(int time, boolean isStart) {
44         this.time = time;
45         this.isStart = isStart;
46     }
47 }

 

转载于:https://www.cnblogs.com/beiyeqingteng/p/6120491.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值