-
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 2008: Maximum Earnings From Taxi
- Loading branch information
Showing
3 changed files
with
86 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
51 changes: 51 additions & 0 deletions
51
src/problem_2008_maximum_earnings_from_taxi/dynamic_programming.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,51 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn max_taxi_earnings(n: i32, rides: Vec<Vec<i32>>) -> i64 { | ||
let n = n as u32 as usize; | ||
|
||
let mut rides = rides | ||
.into_iter() | ||
.map(|ride| { | ||
let [start, end, tip]: [_; 3] = ride.try_into().ok().unwrap(); | ||
|
||
(end as u32 - 1, start as u32 - 1, tip as u32) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
rides.sort_unstable_by_key(|&(end, _, _)| end); | ||
|
||
let mut cache = vec![0_u64; n].into_boxed_slice(); | ||
let mut prev_position = 0; | ||
let mut prev_earning = 0_u64; | ||
|
||
for (end, start, tip) in rides { | ||
if end != prev_position { | ||
cache[prev_position as usize..end as usize].fill(prev_earning); | ||
} | ||
|
||
prev_earning = prev_earning.max(cache[start as usize] + u64::from(end - start + tip)); | ||
prev_position = end; | ||
} | ||
|
||
prev_earning as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn max_taxi_earnings(n: i32, rides: Vec<Vec<i32>>) -> i64 { | ||
Self::max_taxi_earnings(n, rides) | ||
} | ||
} | ||
|
||
#[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,34 @@ | ||
pub mod dynamic_programming; | ||
|
||
pub trait Solution { | ||
fn max_taxi_earnings(n: i32, rides: Vec<Vec<i32>>) -> i64; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((5, &[[2, 5, 4], [1, 5, 1]] as &[_]), 7), | ||
( | ||
( | ||
20, | ||
&[ | ||
[1, 6, 1], | ||
[3, 10, 2], | ||
[10, 12, 3], | ||
[11, 12, 2], | ||
[12, 15, 2], | ||
[13, 18, 1], | ||
], | ||
), | ||
20, | ||
), | ||
]; | ||
|
||
for ((n, rides), expected) in test_cases { | ||
assert_eq!(S::max_taxi_earnings(n, rides.iter().map(Vec::from).collect()), expected); | ||
} | ||
} | ||
} |