1. Track Rooms per Meeting (Brute Force)
Intuition
Sort intervals by start time. Maintain a list of active room end times. For each meeting, check existing rooms. If a room has end <= meeting.start, reuse it and update end. Otherwise, allocate a new room.
When to apply: Use to understand room reuse semantics.
Brute Force Room Assignment Trace
2 of 2 questions left todayIntervals: [[0, 30], [5, 10], [15, 20]]
Intervals: [[0, 30], [5, 10], [15, 20]]. Sorted by start: [[0, 30], [5, 10], [15, 20]].
import java.util.*; public class Main { public static class Solution { public int minMeetingRooms(int[][] intervals) { if (intervals == null || intervals.length == 0) return 0; Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); List<Integer> rooms = new ArrayList<>(); for (int[] interval : intervals) { boolean allocated = false; for (int i = 0; i < rooms.size(); i++) { if (rooms.get(i) <= interval[0]) { rooms.set(i, interval[1]); allocated = true; break; } } if (!allocated) { rooms.add(interval[1]); } } return rooms.size(); } } public static void main(String[] args) { Solution sol = new Solution(); int[][] intervals = {{0, 30}, {5, 10}, {15, 20}}; System.out.println("Min meeting rooms: " + sol.minMeetingRooms(intervals)); }}Time & Space Complexity
- Time complexity:
O(n^2)— Sorting takes O(n log n). For each of n meetings, scanning up to n existing rooms takes O(n2). - Space complexity:
O(n)— List of rooms stores up to n end times.
Why Brute Force Doesn't Scale:
For N = 10,000, N2 requires 100 million comparisons.
Two pointers sorting requires only ~140,000 operations (700x faster) and avoids heap node allocations.