':type courses: List[List[int]]
:rtype: int'

def scheduleCourse(self, courses):

    courses.sort(key=(lambda (t, end): end))
    max_heap = []
    now = 0
    for (t, end) in courses:
        now += t
        heapq.heappush(max_heap, (- t))
        if (now > end):
            now += heapq.heappop(max_heap)
    return len(max_heap)
