Skip to content

Commit 6780dbe

Browse files
committed
253_Meeting_Rooms_II
1 parent 7644927 commit 6780dbe

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Also, there are open source implementations for basic data structs and algorithm
9191
| 246 | [Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/246_Strobogrammatic_Number.py) | Hash table and reverse string, O(n) and O(n) |
9292
| 249 | [Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/249_Group_Shifted_Strings.py) | Hash and generate hash code for each string, O(n) and O(n) |
9393
| 252 | [Meeting Rooms](https://leetcode.com/problems/meeting-rooms/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/252_Meeting_Rooms.py) | 1. Sort and compare intervals[i].end with intervals[i+1], O(nlogn) and O(1)<br>2. Sort and check intervals when count >= 2, O(nlogn) and O(n) |
94+
| 253 | [Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/253_Meeting_Rooms_II.py) &hearts; [Java](https://github.com/qiyuangong/leetcode/blob/master/java/253_Meeting_Rooms_II.java) | 1. Priority queue and sort, O(nlogn) and O(n)<br>2. Go through timeline. If it's a start then meeting + 1, else meeting - 1. The ans is the max(meeting) in timeline. O(nlogn) and O(n) |
9495
| 259 | [3Sum Smaller](https://leetcode.com/problems/3sum-smaller/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/259_3Sum_Smaller.py) | 1. Reduce to two sum smaller, then binary search, O(n^2lgn) and O(1)<br>2. Reduce to two sum smaller, then two points, O(n^2) and O(1)|
9596
| 266 | [Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/266_Palindrome_Permutation.py) | Compute frequency, check number of odd occurrences <= 1 then palindrome, O(n) and O(n)|
9697
| 267 | [Palindrome Permutation II](https://leetcode.com/problems/palindrome-permutation-ii/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/267_Palindrome_Permutation_II.py) | Check palindrome then generate half with [Permutations II](https://leetcode.com/problems/permutations-ii/), O(n^2) and O(n^2) |

java/253_Meeting_Rooms_II.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Definition for an interval. public class Interval { int start; int end; Interval() { start = 0;
3+
* end = 0; } Interval(int s, int e) { start = s; end = e; } }
4+
*/
5+
class Solution {
6+
7+
/* public int minMeetingRooms(Interval[] intervals) {
8+
9+
// Check for the base case. If there are no intervals, return 0
10+
if (intervals.length == 0) {
11+
return 0;
12+
}
13+
// Min heap
14+
PriorityQueue<Integer> allocator =
15+
new PriorityQueue<Integer>(
16+
intervals.length,
17+
new Comparator<Integer>() {
18+
public int compare(Integer a, Integer b) {
19+
return a - b;
20+
}
21+
});
22+
23+
// Sort the intervals by start time
24+
Arrays.sort(
25+
intervals,
26+
new Comparator<Interval>() {
27+
public int compare(Interval a, Interval b) {
28+
return a.start - b.start;
29+
}
30+
});
31+
// Add the first meeting
32+
allocator.add(intervals[0].end);
33+
34+
// Iterate over remaining intervals
35+
for (int i = 1; i < intervals.length; i++) {
36+
// If the room due to free up the earliest is free, assign that room to this meeting.
37+
if (intervals[i].start >= allocator.peek()) {
38+
allocator.poll();
39+
}
40+
// If a new room is to be assigned, then also we add to the heap,
41+
// If an old room is allocated, then also we have to add to the heap with updated end time.
42+
allocator.add(intervals[i].end);
43+
}
44+
// The size of the heap tells us the minimum rooms required for all the meetings.
45+
return allocator.size();
46+
}*/
47+
48+
public int minMeetingRooms(Interval[] intervals) {
49+
int ans = 0, curr = 0;
50+
List<TimePoint> timeline = new ArrayList<>();
51+
for (Interval interval: intervals) {
52+
timeline.add(new TimePoint(interval.start, 1));
53+
timeline.add(new TimePoint(interval.end, -1));
54+
}
55+
timeline.sort(new Comparator<TimePoint>() {
56+
public int compare(TimePoint a, TimePoint b) {
57+
if (a.time != b.time) return a.time - b.time;
58+
else return a.room - b.room;
59+
}
60+
});
61+
for (TimePoint t: timeline) {
62+
curr += t.room;
63+
if (curr >= ans) ans = curr;
64+
}
65+
return ans;
66+
}
67+
68+
private class TimePoint {
69+
int time;
70+
int room;
71+
72+
TimePoint(int time, int room) {
73+
this.time = time;
74+
this.room = room;
75+
}
76+
}
77+
}

python/253_Meeting_Rooms_II.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Definition for an interval.
2+
# class Interval(object):
3+
# def __init__(self, s=0, e=0):
4+
# self.start = s
5+
# self.end = e
6+
7+
8+
# class Solution(object):
9+
# def minMeetingRooms(self, intervals):
10+
# """
11+
# :type intervals: List[Interval]
12+
# :rtype: int
13+
# """
14+
# # If there is no meeting to schedule then no room needs to be allocated.
15+
# if not intervals:
16+
# return 0
17+
# # The heap initialization
18+
# free_rooms = []
19+
# # Sort the meetings in increasing order of their start time.
20+
# intervals.sort(key= lambda x: x.start)
21+
# # Add the first meeting. We have to give a new room to the first meeting.
22+
# heapq.heappush(free_rooms, intervals[0].end)
23+
# # For all the remaining meeting rooms
24+
# for i in intervals[1:]:
25+
# # If the room due to free up the earliest is free, assign that room to this meeting.
26+
# if free_rooms[0] <= i.start:
27+
# heapq.heappop(free_rooms)
28+
# # If a new room is to be assigned, then also we add to the heap,
29+
# # If an old room is allocated, then also we have to add to the heap with updated end time.
30+
# heapq.heappush(free_rooms, i.end)
31+
# # The size of the heap tells us the minimum rooms required for all the meetings.
32+
# return len(free_rooms)
33+
34+
35+
class Solution(object):
36+
def minMeetingRooms(self, intervals):
37+
"""
38+
:type intervals: List[Interval]
39+
:rtype: int
40+
"""
41+
timeline = []
42+
for interval in intervals:
43+
# meeting root + 1
44+
timeline.append((interval.start, 1))
45+
# meeting root - 1
46+
timeline.append((interval.end, -1))
47+
# sort by time
48+
timeline.sort()
49+
ans = curr = 0
50+
# go through timeline
51+
for _, v in timeline:
52+
curr += v
53+
# max meeting room used at this point
54+
ans = max(ans, curr)
55+
return ans

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy