2406. Divide Intervals Into Minimum Number of Groups (Leetcode Medium)

preview_player
Показать описание
Larry solves and analyzes this Leetcode problem as both an interviewer and an interviewee. This is a live recording of a real engineer solving a problem live - no cuts or edits!

#leetcode #coding #programming
Рекомендации по теме
Комментарии
Автор

This is really a very good approach Larry

rajesh-kaipa
Автор

Hi Larry, this could be solved with a minHeap as well, but I feel that your solution is much simpler and I overcomplicated it a bit. What do you think?
class Solution:
def minGroups(self, intervals: List[List[int]]) -> int:
intervals.sort(key = lambda x: (x[0], x[1]))
res = 1
minHeap = []
heapq.heappush(minHeap, intervals[0][1])
for start, end in intervals[1:]:
if start > minHeap[0]:
heapq.heappop(minHeap)
else:
res += 1
heapq.heappush(minHeap, end)
return res
# Time: O(n*log(n)) because the sorting time complexity dominates the time complexity of the minHeap
# Space: O(k) k is the number of groups

gabrielepolenta
Автор

Hi Larry thank you for this explanation, I am still confused why do we need sorting here answer is just max intersection so why sort?

rahulpalve
Автор

Was definitely not smart enough to come up with this.. Had used sorting and then sorted list to find nearest end to map with current start

narolavarshil
join shbcf.ru