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.
Given [[0, 30],[5, 10],[15, 20]], return 2.
Given [[7, 10],[2, 4]], return 1.
Given [[7, 10],[2, 7]], return 1.
解题思路:
算法的实现,遍历intervals中的每一项t,然后对于左边的坐标用[t[0], 1]表示(表示我们进入一个线段),右边的坐标用[t[1], -1]表示(表示我们退出了一个线段),然后将这些新得到的区间加入到一个tmp数组中,对这个数组排序,接着遍历这个数组,遍历的过程中累加我们建立的标记位(也就是前面建立的1和-1)记录累加的最大值即可。
注意:
1. 这道题使用Hashmap存储会导致无序的统计,不会得到正确结果
public int minMeetingRooms(int[][] intervals) {
TreeMap map = new TreeMap<>();
for(int[] interval: intervals) {
int count = map.getOrDefault(interval[0], 0);
map.put(interval[0], count+1);
count = map.getOrDefault(interval[1], 0);
map.put(interval[1], count-1);
}
int maxcount = 0;
int count = 0;
for(Map.Entry entry: map.entrySet()) {
count += entry.ge