LeetCode 1184. Distance Between Bus Stops

Problem Statement


use std::mem::swap;

impl Solution {
pub fn distance_between_bus_stops(distance: Vec<i32>, start: i32, destination: i32) -> i32 {
let (mut val, n, mut ans) = (0, distance.len(), 0);
let (mut s, mut d) = (start, destination);
if s > d {
swap(&mut s, &mut d);
}
for i in s..d {
val += distance[i as usize];
}
ans = val; val = 0;
let mut i = d;
while i != s {
val += distance[i as usize];
i = (i + 1) % n as i32;
}
return std::cmp::min(val, ans);
}
}