FastPrepMinimum Classrooms for Courses

Minimum Classrooms for Courses

Google logoGoogle● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

You are given an array courses, where courses[i] = [starti, endi] is the scheduled time of the i-th course.

Each classroom can host at most one course at a time. A classroom becomes available immediately when a course ends, so a course ending at time t does not overlap a course starting at time t.

Return the minimum number of classrooms required to hold every course at its scheduled time.

Function

minimumClassrooms(courses: int[][]) → int

Examples

Example 1

courses = [[0,30],[5,10],[15,20]]return = 2

The courses [0,30] and [5,10] overlap, so at least two classrooms are necessary. Two are also sufficient.

Example 2

courses = [[1,5],[5,8],[8,9]]return = 1

Every course starts when or after the previous course ends, so one classroom can be reused.

Example 3

courses = [[4,10],[4,10],[4,10]]return = 3

All three courses run at the same time, so each needs a separate classroom.

Constraints

  • 1 <= courses.length <= 100000.
  • courses[i].length == 2.
  • 0 <= starti < endi <= 1000000000.

More Google problems

See Google hiring insights
public int minimumClassrooms(int[][] courses) {
    // Write your code here.
}
courses[[0,30],[5,10],[15,20]]
expected2
Checking account…