LeetCode每日一题(1904. The Number of Full Rounds You Have Played)

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.

本质是计算开始时间点和结束时间点之间所覆盖的整刻数范围,即 xx:00-xx:15, xx:15-xx:30, xx:30-xx:45, xx:45-xx:60。

首先想到的应该是将 login_time 和 logout_time 都转换为有效的整刻时间, login_time 向后查找第一个整刻时间, 例如: 12:01 的有效整刻时间应该是 12:15, 因为 12:00 的那轮比赛是参加不了的。logout_time 向前查找, 例如: 12:14 应该转换为 12:00, 因为 12:00 这一轮实际是没有完成的。

第二个问题是 login_time > logout_time 的问题, 即时间经过 00:00, 这时只需分别计算 login_time 到 00:00 和 00:00 到 logout_time 所覆盖的整刻时间段即可

最后是时间间隔不足 15 分钟的情况, 当 login_time 和 logout_time 之间的时间间隔不足 15 分钟时, 我们对 login_time 和 logout_time 的转换会导致 login_time > logtout_time, 比如 login_time = “00:47”, logout_time = “00:57”, 我们转换过后会发现 login_time = “01:00”, logout_time = “00:45”, 这样会导致我们误判整个参赛时间是穿过午夜的, 所以我们需要对这种时间间隔小于 15 分钟的情况进行检查


impl Solution {
    pub fn number_of_rounds(login_time: String, logout_time: String) -> i32 {
        let login: Vec<String> = login_time.split(':').map(str::to_owned).collect();
        let mut login_hour = login[0].parse::<i32>().unwrap();
        let mut login_minute = login[1].parse::<i32>().unwrap();
        let logout: Vec<String> = logout_time.split(':').map(str::to_owned).collect();
        let mut logout_hour = logout[0].parse::<i32>().unwrap();
        let mut logout_minute = logout[1].parse::<i32>().unwrap();
        // 如果登录时间小于登出时间, 并且登出时间-登录时间<15分钟, 一定无法完成任何一轮比赛
        if login_hour < logout_hour || login_hour == logout_hour && login_minute < logout_minute {
            if (logout_hour - login_hour) * 60 + logout_minute - login_minute < 15 {
                return 0;
            }
        }
        // 将非整刻钟的登录时间向后调整至第一个整刻钟
        match login_minute {
            m if m > 0 && m < 15 => login_minute = 15,
            m if m > 15 && m < 30 => login_minute = 30,
            m if m > 30 && m < 45 => login_minute = 45,
            m if m > 45 => {
                login_minute = 0;
                login_hour += 1;
                if login_hour == 24 {
                    login_hour = 0;
                }
            }
            _ => {}
        }
        // 将非整刻钟的登出时间向前调整至第一个整刻钟
        match logout_minute {
            m if m < 15 => logout_minute = 0,
            m if m > 15 && m < 30 => logout_minute = 15,
            m if m > 30 && m < 45 => logout_minute = 30,
            m if m > 45 => logout_minute = 45,
            _ => {}
        }
        // 如果登录时间大于登出时间, 则证明跨午夜, 分别计算登录时间和登出时间与00:00的刻数差然后相加即可
        if login_hour > logout_hour || login_hour == logout_hour && login_minute > logout_minute {
            return (24 - login_hour) * 4 - login_minute / 15
                + logout_hour * 4
                + logout_minute / 15;
        }
        // 如果登录时间小于登出时间, 计算登出时间与登录时间之间的刻数差
        if logout_minute < login_minute {
            logout_hour -= 1;
            logout_minute += 60;
        }
        (logout_hour - login_hour) * 4 + (logout_minute - login_minute) / 15
    }
}

你可能感兴趣的:(算法,数据结构,leetcode,算法,职场和发展)