Minimum Number of Operations to Convert Time — Solution
Minimum Number of Operations to Convert Time solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.
Minimum Number of Operations to Convert Time is an easy coding problem on strings and greedy, asked in Amazon and Adobe interviews.
- Difficulty: Easy
- Topics: Strings, Greedy
- Asked at: Amazon, Adobe
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You are given two times in 24-hour HH:MM format, current and correct, where current <= correct.
In one operation you may add 1, 5, 15 or 60 minutes to current. Return the minimum number of operations needed to make current equal correct.
Example 1
Input: current = "02:30", correct = "04:35"
Output: 3
Explanation: Add 60, 60 and 5.
Example 2
Input: current = "11:00", correct = "11:01"
Output: 1
Example 3
Input: current = "09:41", correct = "10:34"
Output: 7
Explanation: 53 minutes: add 15 three times, then 5 once, then 1 three times.
Constraints
current and correct are valid HH:MM times.current <= correct
Hints for Minimum Number of Operations to Convert Time
Hint 1
Convert both times to minutes since midnight and work with the difference.
Hint 2
Each increment divides the next larger one — 1 | 5 | 15 | 60 — which makes greedy optimal.
Hint 3
Take as many 60s as possible, then 15s, then 5s, then 1s.
How to solve Minimum Number of Operations to Convert Time
The coin denominations 1, 5, 15 and 60 form a chain where each divides the next, so the greedy "largest first" strategy gives the minimum number of coins.
Approach
- Parse both times into minutes:
HH * 60 + MM. - Take the difference
d = correct - current. - For each step in
[60, 15, 5, 1], addd / stepto the answer and setd = d % step. - Return the total.
Why it works
For a canonical coin system where each denomination divides the next, greedy is provably optimal: using a smaller coin where a larger one fits would need at least as many coins, because the larger denomination is an exact multiple of the smaller.
Complexity
- Time —
O(1) - Space —
O(1)
Pitfalls
- Parsing the hour and minute with the wrong slice offsets — the colon sits at index 2.
- Greedy would not be safe for an arbitrary denomination set; it works here only because of the divisibility chain.
- The problem guarantees
current <= correct, so the difference is never negative.
Reference solution
Python
def convertTime(current: str, correct: str) -> int:
def mins(t):
return int(t[0:2]) * 60 + int(t[3:5])
diff = mins(correct) - mins(current)
ops = 0
for step in (60, 15, 5, 1):
ops += diff // step
diff %= step
return opsJava
public static int convertTime(String current, String correct) {
int a = Integer.parseInt(current.substring(0, 2)) * 60 + Integer.parseInt(current.substring(3, 5));
int b = Integer.parseInt(correct.substring(0, 2)) * 60 + Integer.parseInt(correct.substring(3, 5));
int diff = b - a;
int ops = 0;
int[] steps = { 60, 15, 5, 1 };
for (int s : steps) {
ops += diff / s;
diff %= s;
}
return ops;
}C++
int convertTime(string current, string correct) {
int a = stoi(current.substr(0, 2)) * 60 + stoi(current.substr(3, 2));
int b = stoi(correct.substr(0, 2)) * 60 + stoi(correct.substr(3, 2));
int diff = b - a;
int ops = 0;
int steps[4] = { 60, 15, 5, 1 };
for (int i = 0; i < 4; i++) {
ops += diff / steps[i];
diff %= steps[i];
}
return ops;
}JavaScript
var convertTime = function(current, correct) {
function mins(t) {
return Number(t.slice(0, 2)) * 60 + Number(t.slice(3, 5));
}
let diff = mins(correct) - mins(current);
let ops = 0;
const steps = [60, 15, 5, 1];
for (let i = 0; i < steps.length; i++) {
ops += Math.floor(diff / steps[i]);
diff %= steps[i];
}
return ops;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings · Greedy Algorithms