-
Notifications
You must be signed in to change notification settings - Fork 1
/
MeetingRooms.java
50 lines (37 loc) · 1.12 KB
/
MeetingRooms.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.leetcode;
import java.util.PriorityQueue;
public class MeetingRooms {
//https://leetcode.com/problems/meeting-rooms/
class Solution {
public boolean canAttendMeetings(int[][] intervals) {
PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b)->(a[0] - b[0]));
for(int[] i : intervals){
minHeap.offer(i);
}
int[] prev = new int[]{-1,-1};
while(minHeap.size() >0){
int next[] = minHeap.poll();
//as sorted due to heap prev[0] <= next[0] always
if(next[0] < prev[1] ) return false;
prev = next;
}
return true;
}
}
//Alternate , less memory
class Solution2 {
public boolean canAttendMeetings(int[][] intervals) {
Arrays.sort(intervals, (a,b) -> (a[0]-b[0]));
for(int i = 0 ; i < intervals.length - 1 ; i++){
if(intervals[i][1] > intervals[i+1][0]) return false;
}
return true;
}
}
/**
|---------|
|---------|
|-------|
|-----------|
**/
}