返回介绍

solution / 2200-2299 / 2224.Minimum Number of Operations to Convert Time / README_EN

发布于 2024-06-17 01:03:08 字数 4003 浏览 0 评论 0 收藏 0

2224. Minimum Number of Operations to Convert Time

中文文档

Description

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.

 

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.

 

Constraints:

  • current and correct are in the format "HH:MM"
  • current <= correct

Solutions

Solution 1

class Solution:
  def convertTime(self, current: str, correct: str) -> int:
    a = int(current[:2]) * 60 + int(current[3:])
    b = int(correct[:2]) * 60 + int(correct[3:])
    ans, d = 0, b - a
    for i in [60, 15, 5, 1]:
      ans += d // i
      d %= i
    return ans
class Solution {
  public int convertTime(String current, String correct) {
    int a = Integer.parseInt(current.substring(0, 2)) * 60
      + Integer.parseInt(current.substring(3));
    int b = Integer.parseInt(correct.substring(0, 2)) * 60
      + Integer.parseInt(correct.substring(3));
    int ans = 0, d = b - a;
    for (int i : Arrays.asList(60, 15, 5, 1)) {
      ans += d / i;
      d %= i;
    }
    return ans;
  }
}
class Solution {
public:
  int convertTime(string current, string correct) {
    int a = stoi(current.substr(0, 2)) * 60 + stoi(current.substr(3, 2));
    int b = stoi(correct.substr(0, 2)) * 60 + stoi(correct.substr(3, 2));
    int ans = 0, d = b - a;
    vector<int> inc = {60, 15, 5, 1};
    for (int i : inc) {
      ans += d / i;
      d %= i;
    }
    return ans;
  }
};
func convertTime(current string, correct string) int {
  parse := func(s string) int {
    h := int(s[0]-'0')*10 + int(s[1]-'0')
    m := int(s[3]-'0')*10 + int(s[4]-'0')
    return h*60 + m
  }
  a, b := parse(current), parse(correct)
  ans, d := 0, b-a
  for _, i := range []int{60, 15, 5, 1} {
    ans += d / i
    d %= i
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文