Meeting Rooms Problem Minimum Rooms Required in DSA Go - Time & Space Complexity
We want to find out how the time needed to find the minimum number of meeting rooms changes as the number of meetings grows.
How does the program's work increase when more meetings are added?
Analyze the time complexity of the following code snippet.
func minMeetingRooms(intervals [][]int) int {
starts := make([]int, len(intervals))
ends := make([]int, len(intervals))
for i, interval := range intervals {
starts[i] = interval[0]
ends[i] = interval[1]
}
sort.Ints(starts)
sort.Ints(ends)
rooms, endIndex := 0, 0
for i := 0; i < len(intervals); i++ {
if starts[i] < ends[endIndex] {
rooms++
} else {
endIndex++
}
}
return rooms
}
This code finds the minimum number of meeting rooms needed to hold all meetings without overlap.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting the start and end times arrays.
- How many times: Sorting takes time proportional to n log n, where n is the number of meetings.
- Secondary operation: A single loop through all meetings to count rooms, which runs n times.
As the number of meetings increases, sorting takes more time, growing faster than just going through the meetings once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 * log(10) ≈ 33 operations for sorting + 10 for loop |
| 100 | About 100 * log(100) ≈ 664 operations for sorting + 100 for loop |
| 1000 | About 1000 * log(1000) ≈ 9965 operations for sorting + 1000 for loop |
Pattern observation: Sorting dominates and grows faster than the simple loop, so total work grows roughly like n log n.
Time Complexity: O(n log n)
This means the time needed grows a bit faster than the number of meetings, mainly because sorting takes more time as meetings increase.
[X] Wrong: "The time complexity is just O(n) because we only loop through the meetings once."
[OK] Correct: Sorting the start and end times before looping takes more time, and this step grows faster than the single loop, so it cannot be ignored.
Understanding how sorting affects the time helps you explain your solution clearly and shows you know how to analyze real problems efficiently.
"What if the input meetings were already sorted by start time? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem goal
The problem asks for the minimum number of rooms needed so that no two meetings overlap in the same room.Step 2: Clarify what is being counted
We count how many meetings happen at the same time to find the minimum rooms required.Final Answer:
The minimum number of meeting rooms needed so no meetings overlap -> Option CQuick Check:
Minimum rooms = max overlapping meetings [OK]
- Confusing total meetings with rooms needed
- Ignoring overlapping times
- Thinking earliest start time is the answer
Solution
Step 1: Identify suitable data structure for intervals
Meeting intervals have start and end times, so a slice of slices ([][]int) is ideal to store pairs.Step 2: Check other options
map[int]int is less natural for intervals, string can't hold numeric ranges, chan int is for concurrency, not intervals.Final Answer:
[][]int -> Option AQuick Check:
Intervals stored as pairs in [][]int [OK]
- Using map which doesn't keep order
- Trying to store intervals as strings
- Using channels which are unrelated here
intervals := [][]int{{0, 30}, {5, 10}, {15, 20}}
rooms := minMeetingRooms(intervals)
fmt.Println(rooms)
func minMeetingRooms(intervals [][]int) int {
starts := []int{}
ends := []int{}
for _, interval := range intervals {
starts = append(starts, interval[0])
ends = append(ends, interval[1])
}
sort.Ints(starts)
sort.Ints(ends)
rooms, endIndex := 0, 0
for i := 0; i < len(starts); i++ {
if starts[i] < ends[endIndex] {
rooms++
} else {
endIndex++
}
}
return rooms
}Solution
Step 1: Sort start and end times
Starts = [0,5,15], Ends = [10,20,30]Step 2: Iterate and count rooms
At start=0 < end=10, rooms=1; start=5 < end=10, rooms=2; start=15 >= end=10, endIndex++, rooms=2Final Answer:
2 -> Option BQuick Check:
Max overlapping meetings = 2 [OK]
- Not sorting start and end times
- Incrementing rooms incorrectly
- Confusing start and end comparisons
func minMeetingRooms(intervals [][]int) int {
starts := []int{}
ends := []int{}
for _, interval := range intervals {
starts = append(starts, interval[0])
ends = append(ends, interval[1])
}
sort.Ints(starts)
sort.Ints(ends)
rooms := 0
endIndex := 0
for i := 0; i <= len(starts); i++ {
if starts[i] < ends[endIndex] {
rooms++
} else {
endIndex++
}
}
return rooms
}Solution
Step 1: Check loop boundary
The loop uses i <= len(starts), which causes index out-of-range error because valid indices are 0 to len(starts)-1.Step 2: Correct loop condition
Change to i < len(starts) to safely iterate over all elements.Final Answer:
Loop condition should be i < len(starts), not <= to avoid out-of-range -> Option AQuick Check:
Index error fixed by proper loop bounds [OK]
- Using <= instead of < in for loops
- Sorting in wrong order
- Wrong comparison operators
[[1,4],[2,5],[7,9],[6,8],[9,10]], what is the minimum number of meeting rooms required?Solution
Step 1: Sort start and end times
Starts = [1,2,6,7,9], Ends = [4,5,8,9,10]Step 2: Count overlaps to find rooms
i=0 start=1 < end=4 rooms=1 i=1 start=2 < end=4 rooms=2 i=2 start=6 >= end=4 endIndex=1 rooms=2 i=3 start=7 >= end=5 endIndex=2 rooms=2 i=4 start=9 >= end=8 endIndex=3 rooms=2Final Answer:
2 -> Option DQuick Check:
Max overlaps = 2 rooms needed [OK]
- Not sorting intervals before processing
- Miscounting overlaps at boundaries
- Assuming one room per meeting always
Ready to prove it?
Pass the test and this pattern moves unknown
