-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1834: Single-Threaded CPU
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
pub struct Solution; | ||
|
||
use std::collections::BinaryHeap; | ||
|
||
impl Solution { | ||
pub fn get_order(tasks: Vec<Vec<i32>>) -> Vec<i32> { | ||
#[derive(Eq, PartialEq)] | ||
struct Helper(i32, i32, usize); | ||
|
||
impl Ord for Helper { | ||
fn cmp(&self, other: &Self) -> std::cmp::Ordering { | ||
(self.1, self.2).cmp(&(other.1, other.2)).reverse() | ||
} | ||
} | ||
|
||
impl PartialOrd for Helper { | ||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
let mut result = Vec::with_capacity(tasks.len()); | ||
let mut heap = BinaryHeap::<Helper>::with_capacity(tasks.len()); | ||
let mut tasks = tasks | ||
.into_iter() | ||
.enumerate() | ||
.map(|(idx, nums)| (nums[0], nums[1], idx)) | ||
.collect::<Vec<_>>(); | ||
tasks.sort_unstable(); | ||
|
||
let mut current = 0; | ||
|
||
'task_for: for task in tasks { | ||
while task.0 > current { | ||
if let Some(helper) = heap.pop() { | ||
result.push(helper.2 as _); | ||
current += helper.1; | ||
} else { | ||
result.push(task.2 as _); | ||
current = task.0 + task.1; | ||
continue 'task_for; | ||
} | ||
} | ||
heap.push(Helper(task.0, task.1, task.2)); | ||
} | ||
|
||
while let Some(helper) = heap.pop() { | ||
result.push(helper.2 as _); | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
impl super::Solution for Solution { | ||
fn get_order(tasks: Vec<Vec<i32>>) -> Vec<i32> { | ||
Self::get_order(tasks) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
pub mod binary_heap; | ||
|
||
pub trait Solution { | ||
fn get_order(tasks: Vec<Vec<i32>>) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
( | ||
&[[1, 2], [2, 4], [3, 2], [4, 1]] as &[_], | ||
&[0, 2, 3, 1] as &[_], | ||
), | ||
( | ||
&[[7, 10], [7, 12], [7, 5], [7, 4], [7, 2]], | ||
&[4, 3, 2, 0, 1], | ||
), | ||
( | ||
&[ | ||
[19, 13], | ||
[16, 9], | ||
[21, 10], | ||
[32, 25], | ||
[37, 4], | ||
[49, 24], | ||
[2, 15], | ||
[38, 41], | ||
[37, 34], | ||
[33, 6], | ||
[45, 4], | ||
[18, 18], | ||
[46, 39], | ||
[12, 24], | ||
], | ||
&[6, 1, 2, 9, 4, 10, 0, 11, 5, 13, 3, 8, 12, 7], | ||
), | ||
]; | ||
|
||
for (tasks, expected) in test_cases { | ||
assert_eq!( | ||
S::get_order(tasks.iter().map(Vec::from).collect()), | ||
expected | ||
); | ||
} | ||
} | ||
} |