oneby7

Intervals Merging and scheduling

510. Meeting Rooms II

Medium

You are given an array intervals, where intervals[i] = [start, end] is a meeting that begins at time start and ends at time end. Each meeting needs a room for its whole length, and a room holds one meeting at a time. Return the fewest rooms that can hold every meeting.

A meeting uses its room from start up to end. A meeting may begin at the moment another one ends and take over its room, so [1,4] and [4,6] can share a room.

Example 1

Input: intervals = [[8, 14], [1, 6], [3, 9], [10, 12], [5, 7]]

Output: 3

Explanation: At time 5, the meetings [1,6], [3,9] and [5,7] are all running, so they need three rooms. Three are enough: [8,14] takes the room [1,6] left, and [10,12] takes the room [5,7] left.

Example 2

Input: intervals = [[4, 7], [1, 4], [7, 9]]

Output: 1

Explanation: Each meeting begins the moment the one before it ends, so one room holds all three.

Example 3

Input: intervals = [[0, 1000000000], [6, 8], [6, 8]]

Output: 3

Explanation: The two copies of [6,8] run at the same time, and both run inside the long meeting. Each of the three needs its own room.

Check your understanding

Work out the answer for this input before you write any code.

intervals = [[3, 6], [1, 4], [3, 9], [6, 8], [4, 7]]

Constraints

  • 1 ≤ intervals.length ≤ 105
  • intervals[i].length = 2
  • 0 ≤ start < end ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.