-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2054: Two Best Non-Overlapping Events
- Loading branch information
Showing
3 changed files
with
85 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
62 changes: 62 additions & 0 deletions
62
src/problem_2054_two_best_non_overlapping_events/iterative.rs
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,62 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::convert::TryInto; | ||
|
||
impl Solution { | ||
fn parse_event(event: &[i32]) -> &[i32; 3] { | ||
event.try_into().ok().unwrap() | ||
} | ||
|
||
pub fn max_two_events(events: Vec<Vec<i32>>) -> i32 { | ||
const PROBE: u64 = 1 << 32; | ||
|
||
let mut events_2 = Vec::with_capacity(events.len() * 2); | ||
|
||
events_2.extend(events.iter().map(|event| { | ||
let &[start, _, value] = Self::parse_event(event); | ||
|
||
(u64::from(start as u32) << 33) | u64::from(value as u32) | ||
})); | ||
|
||
events_2.extend(events.iter().map(|event| { | ||
let &[_, end, value] = Self::parse_event(event); | ||
|
||
(u64::from(end as u32) << 33) | PROBE | u64::from(value as u32) | ||
})); | ||
|
||
drop(events); | ||
|
||
events_2.sort_unstable(); | ||
|
||
let mut result = 0; | ||
let mut max = 0; | ||
|
||
for state in events_2 { | ||
if state & PROBE == 0 { | ||
result = result.max(max + state as u32); | ||
} else { | ||
max = max.max(state as u32); | ||
} | ||
} | ||
|
||
result as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn max_two_events(events: Vec<Vec<i32>>) -> i32 { | ||
Self::max_two_events(events) | ||
} | ||
} | ||
|
||
#[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,22 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn max_two_events(events: Vec<Vec<i32>>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[[1, 3, 2], [4, 5, 2], [2, 4, 3]] as &[_], 4), | ||
(&[[1, 3, 2], [4, 5, 2], [1, 5, 5]], 5), | ||
(&[[1, 5, 3], [1, 5, 1], [6, 6, 5]], 8), | ||
]; | ||
|
||
for (events, expected) in test_cases { | ||
assert_eq!(S::max_two_events(events.iter().map(Vec::from).collect()), expected); | ||
} | ||
} | ||
} |