Skip to content

Commit

Permalink
Add problem 2008: Maximum Earnings From Taxi
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jun 19, 2024
1 parent c592005 commit 9150ae4
Show file tree
Hide file tree
Showing 3 changed files with 86 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 @@ -1475,6 +1475,7 @@ pub mod problem_2001_number_of_pairs_of_interchangeable_rectangles;
pub mod problem_2002_maximum_product_of_the_length_of_two_palindromic_subsequences;
pub mod problem_2006_count_number_of_pairs_with_absolute_difference_k;
pub mod problem_2007_find_original_array_from_doubled_array;
pub mod problem_2008_maximum_earnings_from_taxi;
pub mod problem_2011_final_value_of_variable_after_performing_operations;
pub mod problem_2012_sum_of_beauty_in_the_array;
pub mod problem_2016_maximum_difference_between_increasing_elements;
Expand Down
51 changes: 51 additions & 0 deletions src/problem_2008_maximum_earnings_from_taxi/dynamic_programming.rs
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>();
}
}
34 changes: 34 additions & 0 deletions src/problem_2008_maximum_earnings_from_taxi/mod.rs
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);
}
}
}

0 comments on commit 9150ae4

Please sign in to comment.