You are given two strings current and correct representing two 24-hour times. You have to convert current time to correct time by using the minimum number of operations. In one operation, you can increase the time by 1, 5, 15, or 60 minutes. You need to determine the minimum number of operations required.
current and correct are valid 24-hour times in the “HH:MM” format.correct and current.def convertTime(current: str, correct: str) -> int:
# Helper function to convert "HH:MM" to total minutes since the start of the day
def toMinutes(time_str):
hours, minutes = map(int, time_str.split(":"))
return hours * 60 + minutes
# Calculate the total minutes for current and correct time
current_minutes = toMinutes(current)
correct_minutes = toMinutes(correct)
# Calculate the difference in minutes
delta = correct_minutes - current_minutes
# List of minute increments for operations, in descending order
operations = [60, 15, 5, 1]
# Count the number of operations
num_operations = 0
# Apply the largest operations first
for op in operations:
if delta == 0:
break
num_operations += delta // op
delta %= op
return num_operations
# Example usage:
print(convertTime("09:30", "10:00")) # Output: 1
print(convertTime("11:00", "11:01")) # Output: 1
print(convertTime("23:59", "00:00")) # Output: 1
toMinutes computes total minutes from “00:00” for given time strings.current and correct.This ensures that our solution is both efficient and easy to understand.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?