Skip to content

Commit 15d892c

Browse files
author
Partho Biswas
committed
850. Rectangle Area II
1 parent 07d5ae4 commit 15d892c

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
690690
|05| [1272. Remove Interval](https://tinyurl.com/syvucrt) | [Python](https://tinyurl.com/wu6rdaw/1272_Remove_Interval.py)| [Art 1](https://tinyurl.com/srv4knp) | Medium | **Line Swap** |
691691
|06| [1288. Remove Covered Intervals](https://tinyurl.com/wc72tcx) | [Python](https://tinyurl.com/wu6rdaw/1288_Remove_Covered_Intervals.py)| [Art 1](https://tinyurl.com/vpp5hnx) | Medium | **Line Swap with Greedy**. A must read solution. A gold mie, learned a lot |
692692
|07| [1229. Meeting Scheduler](https://tinyurl.com/t6vtlph) | [Python](https://tinyurl.com/wu6rdaw/1229_Meeting_Scheduler.py)| [Art 1](https://tinyurl.com/rau4xcb) | Medium | **Line Swap**, learned a lot |
693+
|08| [850. Rectangle Area II](https://tinyurl.com/utc58vb) | [Python](https://tinyurl.com/wu6rdaw/850_Rectangle_Area_II.py)| [Art 1](https://tinyurl.com/t59zg3e), [Vid 1](https://tinyurl.com/sumo5b6), [Art 2](https://tinyurl.com/s27k9o4) | Hard | **Line Swap using heap and Segmet Tree**. TODO: Solve it using Segment Tree |
693694

694695
</p>
695696
</details>

leetcode.com/python/253_Meeting_Rooms_II.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,48 @@
22

33

44
# Solution 01: Official solution from leetcode itself
5+
# Definition for an interval.
6+
class Interval(object):
7+
def __init__(self, s=0, e=0):
8+
self.start = s
9+
self.end = e
10+
11+
12+
# Official solution from leetcode itself
513
class Solution(object):
614
def minMeetingRooms(self, intervals):
715
"""
8-
:type intervals: List[List[int]]
16+
:type intervals: List[Interval]
917
:rtype: int
1018
"""
11-
if len(intervals) < 1:
19+
20+
# If there is no meeting to schedule then no room needs to be allocated.
21+
if not intervals:
1222
return 0
23+
1324
# The heap initialization
14-
usedRooms = []
25+
occupied_rooms = []
1526

16-
# Sort the meetings in increasing order of their start time
27+
# Sort the meetings in increasing order of their start time.
1728
intervals.sort(key= lambda x: x[0])
1829

19-
# Add the first meetinng. we have to give a new room to the first meeting
20-
heapq.heappush(usedRooms, intervals[0][1])
30+
# Add the first meeting. We have to give a new room to the first meeting.
31+
# We are storing the meeting's END time into the heap, So that we can know which meetiing is going to end first and we can assign new meetings aaccordingly
32+
heapq.heappush(occupied_rooms, intervals[0][1])
2133

2234
# For all the remaining meeting rooms
2335
for interval in intervals[1:]:
2436

2537
# If the room due to free up the earliest is free, assign that room to this meeting.
26-
if interval[0] >= usedRooms[0]:
27-
heapq.heappop(usedRooms)
38+
if occupied_rooms[0] <= interval[0]:
39+
heapq.heappop(occupied_rooms)
2840

2941
# If a new room is to be assigned, then also we add to the heap,
3042
# If an old room is allocated, then also we have to add to the heap with updated end time.
31-
heapq.heappush(usedRooms, interval[1])
43+
heapq.heappush(occupied_rooms, interval[1])
44+
45+
return len(occupied_rooms)
3246

33-
# If a new room is to be assigned, then also we add to the heap,
34-
# If an old room is allocated, then also we have to add to the heap with updated end time.
35-
return len(usedRooms)
3647

3748

3849

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Source: https://tinyurl.com/t59zg3e
2+
# Approach 3: Line Sweep
3+
# Time Complexity: O(N^2 log N) , nlogn is for sorting. the intervals and n is for the length. of intervals
4+
# https://tinyurl.com/v9mdkla
5+
class Solution(object):
6+
def rectangleArea(self, rectangles):
7+
"""
8+
:type rectangles: List[List[int]]
9+
:rtype: int
10+
"""
11+
# Pre processing, populate events
12+
OPEN, CLOSE = 0, 1
13+
events = []
14+
for x1, y1, x2, y2 in rectangles:
15+
events.append((y1, OPEN, x1, x2))
16+
events.append((y2, CLOSE, x1, x2))
17+
events.sort()
18+
19+
active = []
20+
prevY = events[0][0]
21+
area = 0
22+
for currY, itervalType, openX1, closeX2 in events:
23+
# For all vertical ground covered, update answer
24+
height = currY - prevY
25+
width = self.mergeInterval(active)
26+
area += width * height
27+
28+
if itervalType is OPEN:
29+
active.append((openX1, closeX2))
30+
active.sort()
31+
else:
32+
active.remove((openX1, closeX2))
33+
34+
prevY = currY
35+
36+
return area % (10 ** 9 + 7)
37+
38+
# This calculates the total horizontal length of our active intervals
39+
def mergeInterval(self, activeIntervals):
40+
width, prevX1 = 0, -1
41+
for x1, x2 in activeIntervals:
42+
prevX1 = max(prevX1, x1)
43+
width += max(0, x2 - prevX1)
44+
prevX1 = max(prevX1, x2)
45+
return width

0 commit comments

Comments
 (0)