539. Minimum Time Difference

Given a list of 24-hour clock time points in "HH:MM" format, return the minimum minutes difference between any two time-points in the list.

 

Example 1:

Input: timePoints = ["23:59","00:00"]
Output: 1

Example 2:

Input: timePoints = ["00:00","23:59","00:00"]
Output: 0

 

Constraints:

  • 2 <= timePoints <= 2 * 104
  • timePoints[i] is in the format "HH:MM".

Rust Solution

struct Solution;

const M: usize = 1440;

impl Solution {
    fn find_min_difference(time_points: Vec<String>) -> i32 {
        let n = time_points.len();
        if n > M {
            return 0;
        }
        let mut a = [false; M];
        for time_point in time_points {
            let t = Self::clock_to_minute(time_point);
            if a[t as usize] {
                return 0;
            } else {
                a[t as usize] = true;
            }
        }
        let mut first: Option<usize> = None;
        let mut last: Option<usize> = None;
        let mut prev: Option<usize> = None;
        let mut min = M;
        for i in 0..M {
            if a[i] {
                if first == None {
                    prev = Some(i);
                    first = Some(i);
                } else {
                    min = usize::min(min, i - prev.unwrap());
                    prev = Some(i);
                    last = Some(i);
                }
            }
        }
        min = usize::min(first.unwrap() + M - last.unwrap() as usize, min);
        min as i32
    }

    fn clock_to_minute(s: String) -> usize {
        let parts: Vec<usize> = s.split(':').map(|s| s.parse::<usize>().unwrap()).collect();
        parts[0] * 60 + parts[1]
    }
}

#[test]
fn test() {
    let time_points = vec_string!["23:59", "00:00"];
    assert_eq!(Solution::find_min_difference(time_points), 1);
}

Having problems with this solution? Click here to submit an issue on github.