Skip to content

Commit dbdbdef

Browse files
committed
add solution and links from problem-253
1 parent 960fc98 commit dbdbdef

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ In order to practice with similar data structures I'll be placing each problem i
108108
- [Merge Intervals #56](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/merge-intervals-56.md)
109109
- [Evaluate Reverse Polish Notation #150](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/polish-notation-150.md)
110110
- [Maximum Units on a Truck #1710](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/max-units-truck-1710.md)
111+
- [Meeting Rooms II](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/meeting-rooms-ii-253.md)
111112

112113
### Queue
113114

@@ -166,6 +167,7 @@ In order to practice with similar data structures I'll be placing each problem i
166167
### Heap
167168

168169
- [K Closest Points to Origin #973](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/k-closest-origin-973.md)
170+
- [Meeting Rooms II](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/meeting-rooms-ii-253.md)
169171

170172
## Algorithm Patterns
171173

@@ -180,6 +182,7 @@ Within the problems above there are several patterns that often occur. I plan to
180182
- [Middle of the Linked List #876](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/middle-linked-list-876.md)
181183
- [Contains Duplicate #217](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/contains-duplicate-217.md)
182184
- [3Sum #15](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/3Sum-15.md)
185+
- [Meeting Rooms II](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/meeting-rooms-ii-253.md)
183186

184187
### Sliding Window
185188

medium/meeting-rooms-ii-253.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Meeting Rooms II
2+
3+
Page on leetcode: https://leetcode.com/problems/meeting-rooms-ii/
4+
5+
## Problem Statement
6+
7+
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.)
8+
9+
### Constraints
10+
11+
### Example
12+
13+
```
14+
Input: intervals = [(0,30),(5,10),(15,20)]
15+
Output: 2
16+
Explanation:
17+
We need two meeting rooms
18+
room1: (0,30)
19+
room2: (5,10),(15,20)
20+
```
21+
22+
## Solution
23+
24+
### Optimized Solution
25+
26+
This problem can be approached using sorting and two pointers or using a heap. The below is with a heap which has a time complexity of O(nlogn) and space complexity of O(n). You can see an explanation of this approach here: https://www.youtube.com/watch?v=qx7Akat3xrM
27+
28+
```javascript
29+
const minMeetingRooms(intervals) {
30+
// Sort meetings by start times
31+
intervals.sort((a,b) => a[0] - b[0])
32+
33+
// Create initial heap
34+
const usedRooms = [intervals[0][1]]
35+
36+
// iterate thru meetings and compare start times to heap
37+
for (let i = 1; i < intervals.length; i++){
38+
// If current meeting has no overlap with the minheap meeting, then remove the minheap meeting. We won't need an additional meeting room.
39+
if (intervals[i][0] >= usedRooms[0]) {
40+
heapifyDown()
41+
}
42+
// Add current meeting to the heap
43+
heapifyUp(intervals[i][1])
44+
}
45+
46+
return usedRooms.length
47+
48+
// Heapify Up helper
49+
function heapifyUp(endTime) {
50+
51+
}
52+
53+
// Heapify Down helper
54+
function heapifyDown(endTime) {
55+
56+
}
57+
58+
}
59+
```

0 commit comments

Comments
 (0)