Skip to main content

630 - Course Schedule III

Details

KeyValue
Linkhttps://leetcode.com/problems/course-schedule-iii/
LanguagePython 3
Runtime1563 ms, faster than 8.47% of Python3 online submissions for Course Schedule III
Memory Usage19.4 MB, less than 90.62% of Python3 online submissions for Course Schedule III
DatastructuresHeap, List[List[int]
AlgorithmsGreedy + Max Heap
ComplexityTime: O(NlogN) Memory: O(N)

Procedure

  1. ...

Code

class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
max_heap, start_day = [], 0

for duration, last_day in sorted(courses, key=lambda c: c[1]):
start_day += duration
heapq.heappush(max_heap, -duration)

if start_day > last_day:
new_duration = heapq.heappop(max_heap)
start_day += new_duration

return len(max_heap)