Skip to content

Commit ac79eca

Browse files
committed
meeting-rooms-ii solution
1 parent a736c22 commit ac79eca

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

meeting-rooms-ii/hyer0705.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Interval } from "../home/lib/index";
2+
/**
3+
* Definition of Interval:
4+
* export class Interval {
5+
* start :number;
6+
* end :number;
7+
* constructor(start :number, end :number) {
8+
* this.start = start;
9+
* this.end = end;
10+
* }
11+
* }
12+
*/
13+
import { MinHeap } from "@datastructures-js/heap";
14+
15+
export class Solution {
16+
/**
17+
* @param intervals: an array of meeting time intervals
18+
* @return: the minimum number of conference rooms required
19+
*/
20+
// Time Complexity: O(n log n)
21+
// Space Complexity: O(n)
22+
minMeetingRooms(intervals: Interval[]): number {
23+
intervals.sort((a, b) => a.start - b.start);
24+
25+
const minHeap = new MinHeap();
26+
const n = intervals.length;
27+
28+
let maximumRooms = 0;
29+
for (let i = 0; i < n; i++) {
30+
const { start, end } = intervals[i];
31+
32+
if (minHeap.isEmpty()) {
33+
minHeap.insert(end);
34+
maximumRooms = Math.max(maximumRooms, minHeap.size());
35+
continue;
36+
}
37+
38+
if (start >= minHeap.root()) {
39+
minHeap.pop();
40+
}
41+
minHeap.insert(end);
42+
maximumRooms = Math.max(maximumRooms, minHeap.size());
43+
}
44+
return maximumRooms;
45+
}
46+
}

0 commit comments

Comments
 (0)