Skip to main content
Back to problems
#2224
Easy Algorithms

Minimum number of operations to convert time

String Greedy
66.3% acceptance
Feb 25, 2026
501
39
You are given two strings current and correct representing two 24-hour times. 24-hour times are formatted as "HH:MM", where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59. In one operation you can increase the time current by 1, 5, 15, or 60 minutes. You can perform this operation any number of times. Return the minimum number of operations needed to convert current to correct.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn convert_time(current: String, correct: String) -> i32 {
    let to_min = |s: &str| -> i32 {
      let h: i32 = s[0..2].parse().unwrap();
      let m: i32 = s[3..5].parse().unwrap();
      h * 60 + m
    };
    let mut diff = to_min(&correct) - to_min(&current);
    let mut ops = 0;
    for &step in &[60, 15, 5, 1] {
      ops += diff / step;
      diff %= step;
    }
    ops
  }
}