Minimum Number of Operations to Convert Time
EasyUpdated: Aug 2, 2025
Practice on:
Problem
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 _theminimum number of operations needed to convert _current
tocorrect.
Examples
Example 1
Input: current = "02:30", correct = "04:35"
Output: 3
Explanation: We can convert current to correct in 3 operations as follows:
- Add 60 minutes to current. current becomes "03:30".
- Add 60 minutes to current. current becomes "04:30".
- Add 5 minutes to current. current becomes "04:35".
It can be proven that it is not possible to convert current to correct in fewer than 3 operations.
Example 2
Input: current = "11:00", correct = "11:01"
Output: 1
Explanation: We only have to add one minute to current, so the minimum number of operations needed is 1.
Constraints
currentandcorrectare in the format"HH:MM"current <= correct
Solution
Method 1 – Greedy (Largest Step First)
Intuition
Convert both times to minutes, then use the largest possible step (60, 15, 5, 1) at each stage to minimize the number of operations.
Approach
- Parse both times to minutes.
- Compute the difference.
- For each step in [60, 15, 5, 1], use as many as possible, subtract, and count.
- Return the total count.
Code
C++
#include <string>
using namespace std;
class Solution {
public:
int convertTime(string current, string correct) {
int c = stoi(current.substr(0,2)) * 60 + stoi(current.substr(3,2));
int t = stoi(correct.substr(0,2)) * 60 + stoi(correct.substr(3,2));
int diff = t - c, ans = 0;
for (int step : {60, 15, 5, 1}) {
ans += diff / step;
diff %= step;
}
return ans;
}
};
Go
import "strconv"
func convertTime(current, correct string) int {
c := atoi(current[:2])*60 + atoi(current[3:])
t := atoi(correct[:2])*60 + atoi(correct[3:])
diff, ans := t-c, 0
for _, step := range []int{60, 15, 5, 1} {
ans += diff / step
diff %= step
}
return ans
}
func atoi(s string) int {
n, _ := strconv.Atoi(s)
return n
}
Java
class Solution {
public int convertTime(String current, String correct) {
int c = Integer.parseInt(current.substring(0,2)) * 60 + Integer.parseInt(current.substring(3,5));
int t = Integer.parseInt(correct.substring(0,2)) * 60 + Integer.parseInt(correct.substring(3,5));
int diff = t - c, ans = 0;
int[] steps = {60, 15, 5, 1};
for (int step : steps) {
ans += diff / step;
diff %= step;
}
return ans;
}
}
Kotlin
class Solution {
fun convertTime(current: String, correct: String): Int {
val c = current.substring(0,2).toInt() * 60 + current.substring(3,5).toInt()
val t = correct.substring(0,2).toInt() * 60 + correct.substring(3,5).toInt()
var diff = t - c
var ans = 0
for (step in listOf(60, 15, 5, 1)) {
ans += diff / step
diff %= step
}
return ans
}
}
Python
class Solution:
def convertTime(self, current: str, correct: str) -> int:
c = int(current[:2]) * 60 + int(current[3:])
t = int(correct[:2]) * 60 + int(correct[3:])
diff, ans = t - c, 0
for step in [60, 15, 5, 1]:
ans += diff // step
diff %= step
return ans
Rust
impl Solution {
pub fn convert_time(current: String, correct: String) -> i32 {
let c = current[0..2].parse::<i32>().unwrap() * 60 + current[3..].parse::<i32>().unwrap();
let t = correct[0..2].parse::<i32>().unwrap() * 60 + correct[3..].parse::<i32>().unwrap();
let mut diff = t - c;
let mut ans = 0;
for step in [60, 15, 5, 1] {
ans += diff / step;
diff %= step;
}
ans
}
}
TypeScript
class Solution {
convertTime(current: string, correct: string): number {
const c = parseInt(current.slice(0,2)) * 60 + parseInt(current.slice(3));
const t = parseInt(correct.slice(0,2)) * 60 + parseInt(correct.slice(3));
let diff = t - c, ans = 0;
for (const step of [60, 15, 5, 1]) {
ans += Math.floor(diff / step);
diff %= step;
}
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(1)— constant work. - 🧺 Space complexity:
O(1)— only a few variables.