Minimum Number of Operations to Convert Time - 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 the minimum number of operations needed to convert current to correct.

Input & Output

Example 1 — Basic Case
$ Input: current = "02:30", correct = "03:00"
Output: 2
💡 Note: Need 30 minutes difference. Use 2 operations of 15 minutes each: 02:30 → 02:45 → 03:00
Example 2 — Mixed Operations
$ Input: current = "11:00", correct = "11:01"
Output: 1
💡 Note: Need 1 minute difference. Use 1 operation of 1 minute: 11:00 → 11:01
Example 3 — Next Day Edge Case
$ Input: current = "23:59", correct = "00:00"
Output: 1
💡 Note: Need 1 minute to go from 23:59 to 00:00 (next day). Use 1 operation of 1 minute.

Constraints

  • current and correct are valid 24-hour times
  • current ≠ correct is not guaranteed (can be equal)

Visualization

Tap to expand
Time Conversion: 02:30 → 03:00Current02:30150 minutesTarget03:00180 minutesDifference: 30 minutesOperations Available:+60, +15, +5, +1 minutesGreedy: Use largest firstSolution:2 operations (2×15 min)🎯 Key: Greedy approach minimizes total operations
Understanding the Visualization
1
Input
Current time 02:30, target time 03:00
2
Process
Calculate 30 minute difference, apply greedy strategy
3
Output
Minimum 2 operations needed
Key Takeaway
🎯 Key Insight: Greedy algorithm works because larger operations never conflict with optimal solutions
Asked in
Amazon 15 Microsoft 12
28.5K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen