def scheduleCourse3(self, courses): """ :type courses: List[List[int]] :rtype: int """ if len(courses) == 0: return 0 courses.sort(key=lambda x: x[1]) selectedT = [] sumT = 0 for course in courses: t, d = course if sumT + t <= d: sumT += t heappush(selectedT, -t) else: if len(selectedT) > 0 and -selectedT[0] > t: sumT = sumT - (-selectedT[0]) + t heappushpop(selectedT, -t) return len(selectedT)
def nlargest(n, iterable): """Find the n largest elements in a dataset. Equivalent to: sorted(iterable, reverse=True)[:n] """ it = iter(iterable) result = list(islice(it, n)) if not result: return result heapify(result) _heappushpop = heappushpop for elem in it: heappushpop(result, elem) result.sort(reverse=True) return result
def minMeetingRooms(self, intervals: List[List[int]]) -> int: intervals.sort(key = lambda intv : intv[0]) q = [] heapify(q) maxcnt, cnt = 0, 0 while len(intervals) > 0: si, ei = intervals.pop(0) nextei = heappushpop(q, ei) while nextei <= si: cnt -= 1 nextei = heappop(q) heappush(q, nextei) cnt += 1 if maxcnt < cnt: maxcnt = cnt return maxcnt