1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| class Solution { public int[] getOrder(int[][] tasks) { int n = tasks.length; int[] ans = new int[n]; int[][] index = new int[n][3]; for (int i = 0; i < n; ++i) index[i] = new int[]{tasks[i][0], tasks[i][1], i}; Arrays.sort(index, (a, b) -> a[0] - b[0]); PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> { if (a[1] != b[1]) return a[1] - b[1]; else return a[2] - b[2]; }); int time = 1, idx = 0, k = 0; while (idx < n) { while (k < n && index[k][0] <= time) q.offer(index[k++]); if (q.isEmpty()) { time = index[k][0]; } else { int[] cur = q.poll(); ans[idx++] = cur[2]; time += cur[1]; } } return ans; } }
|