当前位置: 代码迷 >> 综合 >> Leetcode 2224. Minimum Number of Operations to Convert Time
  详细解决方案

Leetcode 2224. Minimum Number of Operations to Convert Time

热度:31   发布时间:2023-11-26 05:57:49.0

题目

在这里插入图片描述

解法:greedy

  • 把时间转化为分钟
  • 计算出time difference
  • 从60开始从大到小依次计算每个increase可以最多取多少步
class Solution:def convertTime(self, current: str, correct: str) -> int:curr_h,curr_m = int(current.split(':')[0]),int(current.split(':')[1])unix_curr = curr_h * 60 + curr_mcorr_h,corr_m = int(correct.split(':')[0]),int(correct.split(':')[1])unix_corr = corr_h * 60 + corr_m# print(unix_curr,unix_corr)diff = unix_corr - unix_currnum1,remain1 = diff // 60, diff % 60num2,remain2 = remain1 // 15, remain1 % 15num3,remain3 = remain2 // 5, remain2 % 5res = num1 + num2 + num3 + remain3return res

时间复杂度:常数
空间复杂度:常数

  相关解决方案