返回介绍

solution / 1900-1999 / 1904.The Number of Full Rounds You Have Played / README_EN

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

1904. The Number of Full Rounds You Have Played

中文文档

Description

You are participating in an online chess tournament. There is a chess round that starts every 15 minutes. The first round of the day starts at 00:00, and after every 15 minutes, a new round starts.

  • For example, the second round starts at 00:15, the fourth round starts at 00:45, and the seventh round starts at 01:30.

You are given two strings loginTime and logoutTime where:

  • loginTime is the time you will login to the game, and
  • logoutTime is the time you will logout from the game.

If logoutTime is earlier than loginTime, this means you have played from loginTime to midnight and from midnight to logoutTime.

Return _the number of full chess rounds you have played in the tournament_.

Note: All the given times follow the 24-hour clock. That means the first round of the day starts at 00:00 and the last round of the day starts at 23:45.

 

Example 1:

Input: loginTime = "09:31", logoutTime = "10:14"
Output: 1
Explanation: You played one full round from 09:45 to 10:00.
You did not play the full round from 09:30 to 09:45 because you logged in at 09:31 after it began.
You did not play the full round from 10:00 to 10:15 because you logged out at 10:14 before it ended.

Example 2:

Input: loginTime = "21:30", logoutTime = "03:00"
Output: 22
Explanation: You played 10 full rounds from 21:30 to 00:00 and 12 full rounds from 00:00 to 03:00.
10 + 12 = 22.

 

Constraints:

  • loginTime and logoutTime are in the format hh:mm.
  • 00 <= hh <= 23
  • 00 <= mm <= 59
  • loginTime and logoutTime are not equal.

Solutions

Solution 1: Convert to Minutes

We can convert the input strings to minutes $a$ and $b$. If $a > b$, it means that it crosses midnight, so we need to add one day's minutes $1440$ to $b$.

Then we round $a$ up to the nearest multiple of $15$, and round $b$ down to the nearest multiple of $15$. Finally, we return the difference between $b$ and $a$. Note that we should take the larger value between $0$ and $b - a$.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

class Solution:
  def numberOfRounds(self, loginTime: str, logoutTime: str) -> int:
    def f(s: str) -> int:
      return int(s[:2]) * 60 + int(s[3:])

    a, b = f(loginTime), f(logoutTime)
    if a > b:
      b += 1440
    a, b = (a + 14) // 15, b // 15
    return max(0, b - a)
class Solution {
  public int numberOfRounds(String loginTime, String logoutTime) {
    int a = f(loginTime), b = f(logoutTime);
    if (a > b) {
      b += 1440;
    }
    return Math.max(0, b / 15 - (a + 14) / 15);
  }

  private int f(String s) {
    int h = Integer.parseInt(s.substring(0, 2));
    int m = Integer.parseInt(s.substring(3, 5));
    return h * 60 + m;
  }
}
class Solution {
public:
  int numberOfRounds(string loginTime, string logoutTime) {
    auto f = [](string& s) {
      int h, m;
      sscanf(s.c_str(), "%d:%d", &h, &m);
      return h * 60 + m;
    };
    int a = f(loginTime), b = f(logoutTime);
    if (a > b) {
      b += 1440;
    }
    return max(0, b / 15 - (a + 14) / 15);
  }
};
func numberOfRounds(loginTime string, logoutTime string) int {
  f := func(s string) int {
    var h, m int
    fmt.Sscanf(s, "%d:%d", &h, &m)
    return h*60 + m
  }
  a, b := f(loginTime), f(logoutTime)
  if a > b {
    b += 1440
  }
  return max(0, b/15-(a+14)/15)
}
function numberOfRounds(startTime: string, finishTime: string): number {
  const f = (s: string): number => {
    const [h, m] = s.split(':').map(Number);
    return h * 60 + m;
  };
  let [a, b] = [f(startTime), f(finishTime)];
  if (a > b) {
    b += 1440;
  }
  return Math.max(0, Math.floor(b / 15) - Math.ceil(a / 15));
}

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

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

发布评论

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