Approach
Breadth-first search
For Meeting Rooms III, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 40 lines of Java from the credited upstream file 2402.java.
- The implementation visibly relies on sequence storage, work queue.
- 4 loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public int mostBooked(int n, int[][] meetings) {3 record T(long endTime, int roomId) {}4 int[] count = new int[n];5 6 Arrays.sort(meetings, Comparator.comparingInt(meeting -> meeting[0]));7 8 Queue<T> occupied =9 new PriorityQueue<>(Comparator.comparingLong(T::endTime).thenComparingInt(T::roomId));10 Queue<Integer> availableRoomIds = new PriorityQueue<>();11 12 for (int i = 0; i < n; ++i)13 availableRoomIds.offer(i);14 15 for (int[] meeting : meetings) {16 final int start = meeting[0];17 final int end = meeting[1];18 19 20 while (!occupied.isEmpty() && occupied.peek().endTime <= start)21 availableRoomIds.offer(occupied.poll().roomId);22 if (availableRoomIds.isEmpty()) {23 T t = occupied.poll();24 ++count[t.roomId];25 occupied.offer(new T(t.endTime + (end - start), t.roomId));26 } else {27 final int roomId = availableRoomIds.poll();28 ++count[roomId];29 occupied.offer(new T(end, roomId));30 }31 }32 33 int maxIndex = 0;34 for (int i = 0; i < n; ++i)35 if (count[i] > count[maxIndex])36 maxIndex = i;37 return maxIndex;38 }39}40