Skip to content

Commit 0966280

Browse files
committed
meeting rooms ii solution
1 parent a81538c commit 0966280

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import heapq
2+
3+
from typing import (
4+
List,
5+
)
6+
7+
# Definition of Interval
8+
class Interval(object):
9+
def __init__(self, start, end):
10+
self.start = start
11+
self.end = end
12+
13+
14+
class Solution:
15+
def min_meeting_rooms(self, intervals: List[Interval]) -> int:
16+
# ์ด ๋ฌธ์ œ์—์„œ๋Š” ํšŒ์˜ ์‹œ์ž‘๊ณผ ๋ ์‹œ๊ฐ„์ด interval์ด๋ผ๋Š” ๋ณ„๊ฐœ์˜ ๊ฐ์ฒด๋กœ ์ •์˜๋˜์–ด ์žˆ์Œ
17+
# Meeting Rooms ๋ฌธ์ œ์ฒ˜๋Ÿผ, ์‹œ์ž‘ ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ๋ฏธ๋ฆฌ ์ •๋ ฌ
18+
intervals.sort(key=lambda x: x.start)
19+
20+
# ํ˜„์žฌ ํšŒ์˜๊ฐ€ ์ง„ํ–‰์ค‘์ธ ๋ฐฉ ๊ฐฏ์ˆ˜
21+
rooms = []
22+
23+
for current in intervals:
24+
# ์ดˆ๊ธฐ ์„ค์ • - ์‚ฌ์šฉ์ค‘์ธ ๋ฐฉ์ด ์—†๋‹ค๋ฉด, ๊ทธ๋ƒฅ ์ถ”๊ฐ€
25+
# ๋นˆ ๋ฐฐ์—ด์ด๋ฏ€๋กœ heappush๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๋Š” ์—†๋‹ค
26+
if not rooms:
27+
rooms.append(current.end)
28+
else:
29+
# ํ˜„์žฌ ์ง„ํ–‰ ์ค‘์ธ ํšŒ์˜ ์ค‘ ๊ฐ€์žฅ ๋นจ๋ฆฌ ๋๋‚˜๋Š” ์‹œ๊ฐ„์„ ์ถ”์ถœ
30+
ending = heapq.heappop(rooms)
31+
32+
# ํ˜„์žฌ ํšŒ์˜์˜ ์‹œ์ž‘ ์‹œ๊ฐ„์ด ๊ฐ€์žฅ ๋นจ๋ฆฌ ๋๋‚˜๋Š” ํšŒ์˜๋ณด๋‹ค ๋น ๋ฅด๋‹ค๋ฉด, ํ•ด๋‹น ์ž๋ฆฌ๋ฅผ ๋น„์šธ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋‹ค์‹œ ํž™์— ์‚ฝ์ž…
33+
if current.start < ending:
34+
heapq.heappush(rooms, ending)
35+
36+
# ํ˜„์žฌ ํšŒ์˜๊ฐ€ ๋๋‚˜๋Š” ์‹œ๊ฐ„ ์‚ฝ์ž…
37+
heapq.heappush(rooms, current.end)
38+
39+
# ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ, ๋๋‚˜๋Š” ์‹œ๊ฐ„๋“ค์ด ๋‹ด๊ธด ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ์ตœ์†Œ ํšŒ์˜์‹ค ๊ฐฏ์ˆ˜
40+
return len(rooms)

0 commit comments

Comments
ย (0)