8.27 - hard - 110

630. Course Schedule III

现排序,再用heap,很多greedy的问题都可以这样来做。基本上就是排序,或者是heap,或者两者合用。

class Solution(object):
    def scheduleCourse(self, courses):
        """
        :type courses: List[List[int]]
        :rtype: int
        """
        A = courses
        pq = []
        start = 0
        for t, end in sorted(A, key = lambda (t, end): end):
            start += t
            heapq.heappush(pq, -t)
            while start > end:
                start += heapq.heappop(pq)
        return len(pq)

你可能感兴趣的:(8.27 - hard - 110)