Skip to content

Commit c893463

Browse files
committed
1834
1 parent ee7a3bc commit c893463

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

rust/1834-single-threaded-cpu.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::cmp::Reverse;
2+
use std::collections::BinaryHeap;
3+
4+
impl Solution {
5+
pub fn get_order(tasks: Vec<Vec<i32>>) -> Vec<i32> {
6+
let mut tasks = tasks;
7+
8+
for (i, t) in tasks.iter_mut().enumerate() {
9+
t.push(i as i32);
10+
}
11+
tasks.sort_by(|a, b| a[0].cmp(&b[0]));
12+
13+
let mut res = vec![];
14+
let mut min_heap = BinaryHeap::new();
15+
let (mut i, mut time) = (0, tasks[0][0]);
16+
17+
while !min_heap.is_empty() || i < tasks.len() {
18+
while i < tasks.len() && time >= tasks[i][0] {
19+
min_heap.push(Reverse(vec![tasks[i][1], tasks[i][2]]));
20+
i += 1;
21+
}
22+
23+
if min_heap.is_empty() {
24+
time = tasks[i][0];
25+
} else {
26+
let pair = min_heap.pop().unwrap();
27+
let process_time = pair.0[0];
28+
let index = pair.0[1];
29+
time += process_time;
30+
res.push(index);
31+
}
32+
}
33+
res
34+
}
35+
}

0 commit comments

Comments
 (0)