Skip to main content

Course Schedule III

HardMathSorting

Description

Take maximum number of courses given duration and deadline for each. Courses must finish by deadline. Return the result as an integer.

Examples

Input:courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]
Output:3
Explanation:

Sort by deadline, greedily take shortest courses first: courses 1, 2, 3 fit within deadlines.

Input:courses = [[1]]
Output:1
Explanation:

A single course with duration 1 and deadline 1 can be completed in time, so the maximum number of courses is 1.

Input:courses = [[5,10],[4,6],[7,8],[2,3]]
Output:2
Explanation:

Sort by deadline: [[2,3],[4,6],[7,8],[5,10]]. Take course [2,3] (finishes at time 2). Take course [4,6] (finishes at time 6). Cannot take [7,8] as it would finish at time 13, exceeding deadline 8. Cannot take [5,10] as it would finish at time 18, exceeding deadline 10. Maximum courses = 2.

Constraints

  • 1 ≤ courses.length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.