From 81a6d4f1603c281b267cbf45d1b189e9ddf337cb Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 16 Sep 2024 10:44:47 +0800 Subject: [PATCH] 20240916 finished. --- src/problem/mod.rs | 3 +- .../p1184_distance_between_bus_stops.rs | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/problem/p1184_distance_between_bus_stops.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 389f69b..3263b1b 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -236,4 +236,5 @@ mod p2555_maximize_win_from_two_segments; mod p2576_find_the_maximum_number_of_marked_indices; mod p2398_maximum_number_of_robots_within_budget; mod p2390_removing_stars_from_a_string; -mod p2848_points_that_intersect_with_cars; \ No newline at end of file +mod p2848_points_that_intersect_with_cars; +mod p1184_distance_between_bus_stops; \ No newline at end of file diff --git a/src/problem/p1184_distance_between_bus_stops.rs b/src/problem/p1184_distance_between_bus_stops.rs new file mode 100644 index 0000000..1847555 --- /dev/null +++ b/src/problem/p1184_distance_between_bus_stops.rs @@ -0,0 +1,46 @@ +/** + * [1184] Distance Between Bus Stops + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn distance_between_bus_stops(distance: Vec, start: i32, destination: i32) -> i32 { + let (start, destination) = (start as usize, destination as usize); + let n = distance.len(); + + let mut positive = start; + let mut positive_result = 0; + + while positive != destination { + positive_result += distance[positive]; + positive = (positive + 1) % n; + } + + let mut negative = start; + let mut negative_result = 0; + + while negative != destination { + negative = (negative + n - 1) % n; + negative_result += distance[negative] + } + + positive_result.min(negative_result) + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_1184() { + assert_eq!(1, Solution::distance_between_bus_stops(vec![1,2,3,4], 0, 1)); + assert_eq!(3, Solution::distance_between_bus_stops(vec![1,2,3,4],0, 2)); + assert_eq!(4, Solution::distance_between_bus_stops(vec![1,2,3,4],0,3)); + } +}