-
Notifications
You must be signed in to change notification settings - Fork 0
/
253.meeting-rooms-II.js
85 lines (39 loc) · 1.25 KB
/
253.meeting-rooms-II.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.
Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2
*/
/**
* @param {number[][]} intervals
* @return {number}
*/
var minMeetingRooms = function(intervals) {
};
minMeetingRooms([[0,30],[5,10],[15,20]])
/*
if (intervals==null || intervals.length==0){
return 0
}
if (intervals.length==1){
return 1
}
//sort intervals by start date
intervals.sort((a,b) => a[0] - b[0]);
let rooms = [intervals[0]];
for (let i = 1; i < intervals.length; i++) {
let room = getEarliestRoom(rooms);
let current = intervals[i];
//if the room ends before the current interval starts, use the room and update the room end time to current
if (room[1] <= current[0]){
room[1] = current[1]
}
else{ //allocate a room
rooms.push(current)
}
}
return rooms.length;
function getEarliestRoom(r) { //returns the room that ends first
r.sort((a,b) => a[1]-b[1])
return r[0]
}
*/