LeetCode 2224. Minimum Number of Operations to Convert Time
考点 | 难度 |
---|---|
Greedy | Easy |
题目
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.
思路
转换成分钟,相减得到差,从60开始试。
答案
class Solution(object):
def convertTime(self, current, correct):
current_time = 60 * int(current[0:2]) + int(current[3:5])
target_time = 60 * int(correct[0:2]) + int(correct[3:5])
diff = target_time - current_time
count = 0
for i in [60, 15, 5, 1]:
count += diff // i
diff %= i
return count