forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
27 lines (27 loc) · 850 Bytes
/
Solution.java
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
class Solution {
public int[] getOrder(int[][] tasks) {
int n = tasks.length;
int[][] ts = new int[n][3];
for (int i = 0; i < n; ++i) {
ts[i] = new int[] {tasks[i][0], tasks[i][1], i};
}
Arrays.sort(ts, (a, b) -> a[0] - b[0]);
int[] ans = new int[n];
PriorityQueue<int[]> q
= new PriorityQueue<>((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
int i = 0, t = 0, k = 0;
while (!q.isEmpty() || i < n) {
if (q.isEmpty()) {
t = Math.max(t, ts[i][0]);
}
while (i < n && ts[i][0] <= t) {
q.offer(new int[] {ts[i][1], ts[i][2]});
++i;
}
var p = q.poll();
ans[k++] = p[1];
t += p[0];
}
return ans;
}
}