diff --git a/src/problem/mod.rs b/src/problem/mod.rs index a8d26a3..49646a5 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -492,3 +492,5 @@ mod p1706_where_will_the_ball_fall; mod p1299_replace_elements_with_greatest_element_on_right_side; mod p2080_range_frequency_queries; + +mod p624_maximum_distance_in_arrays; diff --git a/src/problem/p624_maximum_distance_in_arrays.rs b/src/problem/p624_maximum_distance_in_arrays.rs new file mode 100644 index 0000000..50c59a9 --- /dev/null +++ b/src/problem/p624_maximum_distance_in_arrays.rs @@ -0,0 +1,43 @@ +use std::thread::available_parallelism; + +/** + * [624] Maximum Distance in Arrays + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn max_distance(arrays: Vec>) -> i32 { + let mut result = 0; + // -10e4 <= array[i][j] <= 10e4 + let (mut min, mut max) = (10_000 + 1, -10_000 - 1); + + for array in arrays { + result = result + .max(*array.last().unwrap() - min) + .max(max - *array.first().unwrap()); + + min = min.min(*array.first().unwrap()); + max = max.max(*array.last().unwrap()); + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_624() { + assert_eq!( + 4, + Solution::max_distance(vec![vec![1, 2, 3], vec![4, 5], vec![1, 2, 3]]) + ); + assert_eq!(0, Solution::max_distance(vec![vec![1], vec![1]])); + } +}