Skip to content

Commit 8de1dfb

Browse files
committed
LeetCode 252. Meeting Rooms
1 parent f226f33 commit 8de1dfb

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
141141
| [239. Sliding Window Maximum][lc239] | 🔴 Hard | [![python](res/py.png)][lc239py] |
142142
| [240. Search a 2D Matrix II][lc240] | 🟠 Medium | [![python](res/py.png)][lc240py] |
143143
| [242. Valid Anagram][lc242] | 🟢 Easy | [![python](res/py.png)][lc242py] |
144+
| [252. Meeting Rooms][lc252] [🔒][lintcode920] | 🟢 Easy | [![python](res/py.png)][lc252py] |
144145
| [256. Paint House][lc256] [🔒][lintcode515] | 🟠 Medium | [![python](res/py.png)][lc256py] |
145146
| [261. Graph Valid Tree][lc261] [🔒][lintcode178] | 🟠 Medium | [![python](res/py.png)][lc261py] |
146147
| [268. Missing Number][lc268] | 🟢 Easy | [![python](res/py.png)][lc268py] |
@@ -558,6 +559,9 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
558559
[lc240py]: leetcode/search-a-2d-matrix-ii.py
559560
[lc242]: https://leetcode.com/problems/valid-anagram/
560561
[lc242py]: leetcode/valid-anagram.py
562+
[lc252]: https://leetcode.com/problems/meeting-rooms/
563+
[lc252py]: leetcode/meeting-rooms.py
564+
[lintcode920]: https://www.lintcode.com/problem/920/
561565
[lc256]: https://leetcode.com/problems/paint-house/
562566
[lintcode515]: https://www.lintcode.com/problem/515
563567
[lc256py]: leetcode/paint-house.py

leetcode/lists/neetcode.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,21 +494,22 @@ From their website:
494494

495495
## Intervals
496496

497-
| | B75 | Level | Problem | Solutions |
498-
| :-: | --- | --------- | ------------------------------------------------------ | ------------------------------------- |
499-
||| 🟠 Medium | [57. Insert Interval][lc57] | [![python](../../res/py.png)][lc57py] |
500-
||| 🟠 Medium | [56. Merge Intervals][lc56] | [![python](../../res/py.png)][lc56py] |
501-
| || 🟠 Medium | [435. Non-overlapping Intervals][lc435] | |
502-
| || 🟢 Easy | [252. Meeting Rooms][lc252] [🔒][lintcode920] | |
503-
| || 🟠 Medium | [253. Meeting Rooms II][lc253] [🔒][lintcode921] | |
504-
| | | 🔴 Hard | [1851. Minimum Interval to Include Each Query][lc1851] | |
497+
| | B75 | Level | Problem | Solutions |
498+
| :-: | --- | --------- | ------------------------------------------------------ | -------------------------------------- |
499+
||| 🟠 Medium | [57. Insert Interval][lc57] | [![python](../../res/py.png)][lc57py] |
500+
||| 🟠 Medium | [56. Merge Intervals][lc56] | [![python](../../res/py.png)][lc56py] |
501+
| || 🟠 Medium | [435. Non-overlapping Intervals][lc435] | |
502+
| || 🟢 Easy | [252. Meeting Rooms][lc252] [🔒][lintcode920] | [![python](../../res/py.png)][lc252py] |
503+
| || 🟠 Medium | [253. Meeting Rooms II][lc253] [🔒][lintcode921] | |
504+
| | | 🔴 Hard | [1851. Minimum Interval to Include Each Query][lc1851] | |
505505

506506
[lc57]: https://leetcode.com/problems/insert-interval/
507507
[lc57py]: ../insert-interval.py
508508
[lc56]: https://leetcode.com/problems/merge-intervals/
509509
[lc56py]: ../merge-intervals.py
510510
[lc435]: https://leetcode.com/problems/non-overlapping-intervals/
511511
[lc252]: https://leetcode.com/problems/meeting-rooms/
512+
[lc252py]: ../meeting-rooms.py
512513
[lintcode920]: https://www.lintcode.com/problem/920/
513514
[lc253]: https://leetcode.com/problems/meeting-rooms-ii/
514515
[lintcode921]: https://www.lintcode.com/problem/921/

leetcode/meeting-rooms.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 252. Meeting Rooms
2+
# 🟢 Easy
3+
#
4+
# https://leetcode.com/problems/meeting-rooms/
5+
#
6+
# Tags: Array - Sorting - Interval
7+
8+
import timeit
9+
from typing import List
10+
11+
12+
# Definition of Interval:
13+
class Interval(object):
14+
def __init__(self, start, end):
15+
self.start = start
16+
self.end = end
17+
18+
def __repr__(self):
19+
return f"Interval({self.start},{self.end})"
20+
21+
22+
# Sort the intervals, then iterate over them making sure that the start
23+
# time of all intervals is the same or later than the end time of the
24+
# previous interval.
25+
#
26+
# Time complexity: O(n*log(n)) - The sorting step has the highest cost.
27+
# Space complexity: O(n) - sort() in Python can take up to n/2 we could
28+
# implement our ows sorting to bring it down to O(1).
29+
#
30+
# Runtime: 162 ms, faster than 97.00% on LintCode.
31+
# Memory Usage: 9.08 MB, less than 97.00%
32+
class Sorting:
33+
def canAttend(self, intervals: List[Interval]) -> bool:
34+
intervals.sort(key=lambda interval: interval.start)
35+
for i in range(1, len(intervals)):
36+
if intervals[i].start < intervals[i - 1].end:
37+
return False
38+
return True
39+
40+
41+
def test():
42+
executors = [Sorting]
43+
tests = [
44+
[[(5, 8), (9, 15)], True],
45+
[[(0, 8), (8, 10), (15, 20)], True],
46+
[[(0, 30), (5, 10), (15, 20)], False],
47+
]
48+
for executor in executors:
49+
start = timeit.default_timer()
50+
for _ in range(1):
51+
for col, t in enumerate(tests):
52+
sol = executor()
53+
result = sol.canAttend(
54+
[Interval(start, end) for start, end in t[0]]
55+
)
56+
exp = t[1]
57+
assert result == exp, (
58+
f"\033[93m» {result} <> {exp}\033[91m for"
59+
+ f" test {col} using \033[1m{executor.__name__}"
60+
)
61+
stop = timeit.default_timer()
62+
used = str(round(stop - start, 5))
63+
cols = "{0:20}{1:10}{2:10}"
64+
res = cols.format(executor.__name__, used, "seconds")
65+
print(f"\033[92m» {res}\033[0m")
66+
67+
68+
test()

0 commit comments

Comments
 (0)