Approach
Breadth-first search
For Single-Threaded CPU, 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
- 38 lines of Java from the credited upstream file 1834.java.
- The implementation visibly relies on sequence storage, work queue.
- 3 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[] getOrder(int[][] tasks) {3 record T(int procTime, int index) {}4 final int n = tasks.length;5 int[][] A = new int[n][3];6 7 for (int i = 0; i < n; ++i) {8 A[i][0] = tasks[i][0];9 A[i][1] = tasks[i][1];10 A[i][2] = i;11 }12 13 int[] ans = new int[n];14 int ansIndex = 0;15 Queue<T> minHeap =16 new PriorityQueue<>(Comparator.comparingInt(T::procTime).thenComparingInt(T::index));17 int i = 0; 18 long time = 0; 19 20 Arrays.sort(A, Comparator.comparingInt(a -> a[0]));21 22 while (i < n || !minHeap.isEmpty()) {23 if (minHeap.isEmpty())24 time = Math.max(time, (long) A[i][0]);25 while (i < n && time >= (long) A[i][0]) {26 minHeap.offer(new T(A[i][1], A[i][2]));27 ++i;28 }29 final int procTime = minHeap.peek().procTime;30 final int index = minHeap.poll().index;31 time += procTime;32 ans[ansIndex++] = index;33 }34 35 return ans;36 }37}38