#2224
Minimum Number of Operations to Convert Time
pupil · 325 · lc easy +22 · verified · 66.3% accepted · 501 likes · top 71%
Description
You are given two strings current and correct each representing a 24-hour time.
Times follow the format "HH:MM" where HH is in the range 00 to 23 and MM is in 00 to 59. The earliest valid time is 00:00 and the latest is 23:59.
In a single operation you may advance current by exactly 1, 5, 15, or 60 minutes. Perform as many operations as needed.
Return the minimum number of operations required to reach correct from current.
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.
Code
1
2
3