20240916 finished.

This commit is contained in:
jackfiled 2024-09-16 10:44:47 +08:00
parent cae5cd63b0
commit 81a6d4f160
2 changed files with 48 additions and 1 deletions

View File

@ -237,3 +237,4 @@ mod p2576_find_the_maximum_number_of_marked_indices;
mod p2398_maximum_number_of_robots_within_budget; mod p2398_maximum_number_of_robots_within_budget;
mod p2390_removing_stars_from_a_string; mod p2390_removing_stars_from_a_string;
mod p2848_points_that_intersect_with_cars; mod p2848_points_that_intersect_with_cars;
mod p1184_distance_between_bus_stops;

View File

@ -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<i32>, 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));
}
}