forked from prakashshuklahub/Interview-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
252 Meeting Rooms
36 lines (25 loc) · 1.01 KB
/
252 Meeting Rooms
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
Given an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]]
Output: false
Example 2:
Input: intervals = [[7,10],[2,4]]
Output: true
public boolean canAttendMeetings(int[][] intervals) {
if(intervals.length==0)return true;
Arrays.sort(intervals , (a,b)->Integer.compare(a[0],b[0])); //O(nlogn)
Stack<int[]> stack = new Stack(); //O(n)
stack.add(intervals[0]);
for(int i=1;i<intervals.length;i++){
int startpoint2 = intervals[i][0];
int endpoint2 = intervals[i][1];
int[] poparray = stack.pop();
int startpoint1 = poparray[0];
int endpoint1 = poparray[1];
if(endpoint1>startpoint2){
return false;
}
stack.add(intervals[i]);
}
return true;
}