Skip to content

Commit

Permalink
Add problem 1834: Single-Threaded CPU
Browse files Browse the repository at this point in the history
  • Loading branch information
Spxg committed Dec 20, 2024
1 parent 45ef8ac commit 9918f7f
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ pub mod problem_1780_check_if_number_is_a_sum_of_powers_of_three;
pub mod problem_1797_design_authentication_manager;
pub mod problem_1798_maximum_number_of_consecutive_values_you_can_make;
pub mod problem_1807_evaluate_the_bracket_pairs_of_a_string;
pub mod problem_1834_single_threaded_cpu;
pub mod problem_2348_number_of_zero_filled_subarrays;

#[cfg(test)]
Expand Down
67 changes: 67 additions & 0 deletions src/problem_1834_single_threaded_cpu/binary_heap.rs
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>();
}
}
49 changes: 49 additions & 0 deletions src/problem_1834_single_threaded_cpu/mod.rs
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
);
}
}
}

0 comments on commit 9918f7f

Please sign in to comment.