KatInterview

Minimum Classrooms to Schedule All Lectures

Greedy & IntervalsAuto-graded · 22 test cases
MediumAuto-gradedGreedy & Intervalsintervalssweep-linegreedy

Last updated

Tests whether a candidate can reframe a resource-allocation problem as peak interval overlap and handle half-open boundary conditions correctly.

A university needs to assign classrooms to a list of lectures. Each lecture occupies exactly one classroom for its entire duration, and no two lectures can share a classroom at the same time.

Given a list of lectures where each entry is [start, end], return the minimum number of classrooms required to hold every lecture without any conflicts.

Rules

  • lectures[i] = [start, end] with start < end. A classroom is considered occupied from start up to, but not including, end.
  • A classroom that becomes free at time t can immediately host another lecture beginning at time t.
  • Any available classroom can be assigned to any lecture; physical location is not a factor.

Constraints

  • 1 <= len(lectures) <= 10^5
  • 0 <= start < end <= 10^9 for every lecture

Examples

Example 1

Input: min_classrooms([[1, 4], [2, 5], [7, 9], [3, 6]])

Output: 3

Example 2

Input: min_classrooms([[1, 3], [3, 5], [5, 8]])

Output: 1

Example 3

Input: min_classrooms([[2, 10], [3, 4], [4, 6], [7, 8]])

Output: 2

Hints

Hint 1

Think about what the answer actually represents in terms of the lecture intervals — is there a simpler quantity you can compute directly?

Hint 2

Try converting each lecture into two timestamped events (one when it starts, one when it ends) and processing them in order. What does the running total tell you?

Hint 3

When a lecture ends and another begins at the exact same time, the order you process those two events changes the answer. Which order matches the problem rules, and does your sort handle it automatically?