Template:
The intersection of two intervals:
1 | start = max(a.start, b.start) |
252. Meeting Rooms
Leetcode: https://leetcode.com/problems/meeting-rooms/description/
Solution Sort intervals by start first, then see if there’s overlap
1 | def canAttendMeetings(self, intervals: List[List[int]]) -> bool: |
253. Meeting Rooms II
At any point in time we have multiple rooms that can be occupied and we don’t really care which room is free as long as we find one when required for a new meeting.
A naive way to check if a room is available or not is to iterate on all the rooms and see if one is available when we have a new meeting at hand.
1 | def minMeetingRooms(self, intervals: List[List[int]]) -> int: |
452. Minimum Number of Arrows to Burst Balloons
Leetcode: https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/
Video: https://www.youtube.com/watch?v=DguJN47_mSg&ab_channel=HuaHua
One could always track the end of the current balloon, and ignore all the balloons which end before it. Once the current balloon is ended (= the next balloon starts after the current balloon), one has to increase the number of arrows by one and start to track the end of the next balloon.
1 | def findMinArrowShots(self, points: List[List[int]]) -> int: |
56. Merge Intervals
Leetcode: https://leetcode.com/problems/merge-intervals/description/
[Solution] Keep track of the start
and end
, if start < prev.end, update end
to prev.end
, else add interval(start
, end
) to the result list.
1 | class Solution { |
57. Insert Interval
Leetcode: https://leetcode.com/problems/insert-interval/description/
Solution:
- Before the new Interval, push all the intervals into the output array
- Add the new Interval into the output array, if the start of the new interval is smaller than the last tail, merge it with the last one
- Add all the intervals after new interval to the output array
1 | class Solution { |
1 | def insert(self, intervals, newInterval): |
763. Partition Labels
Leetcode: https://leetcode.com/problems/partition-labels/
Note:
- Find the start and end point of each unique character
- Merge Intervals
1 | def partitionLabels(self, S: str) -> List[int]: |
986. Interval List Intersections
Leetcode: https://leetcode.com/problems/interval-list-intersections/
Solution: Use two pointers to select current intervals in A and B. Move pointer that has a smaller end.
1 | var intervalIntersection = function(A, B) { |
Interval Sum Problem
109. Corporate Flight Bookings
Leetcode: https://leetcode.com/problems/corporate-flight-bookings/
Note: For interval [i, j, k]
, we need to + k
in day i
, and would not need k
in day j + 1
. Thus, we could use an accumulation array for the changes of each day.
1 | def corpFlightBookings(self, bookings, n): |
Non-overlapping Intervals
630. Course Schedule III
Leetcode: https://leetcode.com/problems/course-schedule-iii/
Leetcode 715. Range Module
Leetcode: https://leetcode.com/problems/range-module/
Video: https://www.youtube.com/watch?v=pcpB9ux3RrQ&ab_channel=HuaHua
1 | class RangeModule: |
759. Employee Free Time
Leetcode: https://leetcode.com/problems/employee-free-time/
1 | def employeeFreeTime(self, schedule: '[[Interval]]') -> '[Interval]': |
Line Sweeping
731. My Calendar II
Leetcode: https://leetcode.com/problems/my-calendar-ii/
- We will consider ‘start’ time as +1 and ‘end’ time as -1
- If we currently only have ‘start’ and ‘end’ time
- The sum between them will equal to 0, which will balance out
- Now, if we add an overlap between the ‘start/end’ time we will have the following
- s0, s1, e0, e1
- Then the sum will be
- 1 2 1 0
- Since, there is an overlap, we can see that our highest sum is equal to 2
- We can continue this approach to 3 or more overlaps
- Example:
- s0, s1, s2, e0, e1, e3
- 1 2 3 2 1 0
- In this case, our sum has reached 3 and we have found our triple booking
- If we currently only have ‘start’ and ‘end’ time
1 | import bisect |
1094. Car Pooling
Leetcode: https://leetcode.com/problems/car-pooling/
1 | def carPooling(self, trips: List[List[int]], capacity: int) -> bool: |
Leetcode: 218. The Skyline Problem
Good explanation: https://leetcode.com/problems/the-skyline-problem/discuss/61261/10-line-Python-solution-104-ms/208654
1 | def getSkyline(self, buildings: List[List[int]]) -> List[List[int]]: |