GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/253_Meeting_Rooms_II.py at master · qiyuangong/leetcode

Expand file tree

/

253_Meeting_Rooms_II.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
55 lines (51 loc) · 1.96 KB
/

253_Meeting_Rooms_II.py

Copy path

File metadata and controls

55 lines (51 loc) · 1.96 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
# class Solution(object):
# def minMeetingRooms(self, intervals):
# """
# :type intervals: List[Interval]
# :rtype: int
# """
# # If there is no meeting to schedule then no room needs to be allocated.
# if not intervals:
# return 0
# # The heap initialization
# free_rooms = []
# # Sort the meetings in increasing order of their start time.
# intervals.sort(key= lambda x: x.start)
# # Add the first meeting. We have to give a new room to the first meeting.
# heapq.heappush(free_rooms, intervals[0].end)
# # For all the remaining meeting rooms
# for i in intervals[1:]:
# # If the room due to free up the earliest is free, assign that room to this meeting.
# if free_rooms[0] <= i.start:
# heapq.heappop(free_rooms)
# # If a new room is to be assigned, then also we add to the heap,
# # If an old room is allocated, then also we have to add to the heap with updated end time.
# heapq.heappush(free_rooms, i.end)
# # The size of the heap tells us the minimum rooms required for all the meetings.
# return len(free_rooms)
class Solution(object):
def minMeetingRooms(self, intervals):
"""
:type intervals: List[Interval]
:rtype: int
"""
timeline = []
for interval in intervals:
# meeting root + 1
timeline.append((interval.start, 1))
# meeting root - 1
timeline.append((interval.end, -1))
# sort by time
timeline.sort()
ans = curr = 0
# go through timeline
for _, v in timeline:
curr += v
# max meeting room used at this point
ans = max(ans, curr)
return ans
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.