Using greedy without proof
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.
Build confidence with an intuition-first walkthrough focused on greedy fundamentals.
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.
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:
current and correct are in the format "HH:MM"current <= correctProblem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Greedy
"02:30" "04:35"
"11:00" "11:01"
coin-change)design-an-atm-machine)count-days-spent-together)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2224: Minimum Number of Operations to Convert Time
class Solution {
public int convertTime(String current, String correct) {
int a = Integer.parseInt(current.substring(0, 2)) * 60
+ Integer.parseInt(current.substring(3));
int b = Integer.parseInt(correct.substring(0, 2)) * 60
+ Integer.parseInt(correct.substring(3));
int ans = 0, d = b - a;
for (int i : Arrays.asList(60, 15, 5, 1)) {
ans += d / i;
d %= i;
}
return ans;
}
}
// Accepted solution for LeetCode #2224: Minimum Number of Operations to Convert Time
func convertTime(current string, correct string) int {
parse := func(s string) int {
h := int(s[0]-'0')*10 + int(s[1]-'0')
m := int(s[3]-'0')*10 + int(s[4]-'0')
return h*60 + m
}
a, b := parse(current), parse(correct)
ans, d := 0, b-a
for _, i := range []int{60, 15, 5, 1} {
ans += d / i
d %= i
}
return ans
}
# Accepted solution for LeetCode #2224: Minimum Number of Operations to Convert Time
class Solution:
def convertTime(self, current: str, correct: str) -> int:
a = int(current[:2]) * 60 + int(current[3:])
b = int(correct[:2]) * 60 + int(correct[3:])
ans, d = 0, b - a
for i in [60, 15, 5, 1]:
ans += d // i
d %= i
return ans
// Accepted solution for LeetCode #2224: Minimum Number of Operations to Convert Time
/**
* [2224] Minimum Number of Operations to Convert Time
*
* 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.
*
* 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:
*
* current and correct are in the format "HH:MM"
* current <= correct
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/
// discuss: https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn convert_time(current: String, correct: String) -> i32 {
let operation = [60, 15, 5, 1];
let minute = |s: String| -> i32 {
s.split(":")
.map(|x| x.to_string().parse().unwrap())
.reduce(|x, y| 60 * x + y)
.unwrap()
};
let mut delta_time = minute(correct) - minute(current);
let mut result = 0;
for k in operation {
result += delta_time / k;
delta_time %= k;
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2224_example_1() {
let current = "02:30".to_string();
let correct = "04:35".to_string();
let result = 3;
assert_eq!(Solution::convert_time(current, correct), result);
}
#[test]
fn test_2224_example_2() {
let current = "11:00".to_string();
let correct = "11:01".to_string();
let result = 1;
assert_eq!(Solution::convert_time(current, correct), result);
}
}
// Accepted solution for LeetCode #2224: Minimum Number of Operations to Convert Time
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2224: Minimum Number of Operations to Convert Time
// class Solution {
// public int convertTime(String current, String correct) {
// int a = Integer.parseInt(current.substring(0, 2)) * 60
// + Integer.parseInt(current.substring(3));
// int b = Integer.parseInt(correct.substring(0, 2)) * 60
// + Integer.parseInt(correct.substring(3));
// int ans = 0, d = b - a;
// for (int i : Arrays.asList(60, 15, 5, 1)) {
// ans += d / i;
// d %= i;
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.