zoukankan      html  css  js  c++  java
  • [LeetCode] 1904. The Number of Full Rounds You Have Played

    A new online video game has been released, and in this video game, there are 15-minute rounds scheduled every quarter-hour period. This means that at HH:00HH:15HH:30 and HH:45, a new round starts, where HH represents an integer number from 00 to 23. A 24-hour clock is used, so the earliest time in the day is 00:00 and the latest is 23:59.

    Given two strings startTime and finishTime in the format "HH:MM" representing the exact time you started and finished playing the game, respectively, calculate the number of full rounds that you played during your game session.

    • For example, if startTime = "05:20" and finishTime = "05:59" this means you played only one full round from 05:30 to 05:45. You did not play the full round from 05:15 to 05:30 because you started after the round began, and you did not play the full round from 05:45 to 06:00 because you stopped before the round ended.

    If finishTime is earlier than startTime, this means you have played overnight (from startTime to the midnight and from midnight to finishTime).

    Return the number of full rounds that you have played if you had started playing at startTime and finished at finishTime.

    Example 1:

    Input: startTime = "12:01", finishTime = "12:44"
    Output: 1
    Explanation: You played one full round from 12:15 to 12:30.
    You did not play the full round from 12:00 to 12:15 because you started playing at 12:01 after it began.
    You did not play the full round from 12:30 to 12:45 because you stopped playing at 12:44 before it ended.
    

    Example 2:

    Input: startTime = "20:00", finishTime = "06:00"
    Output: 40
    Explanation: You played 16 full rounds from 20:00 to 00:00 and 24 full rounds from 00:00 to 06:00.
    16 + 24 = 40.
    

    Example 3:

    Input: startTime = "00:00", finishTime = "23:59"
    Output: 95
    Explanation: You played 4 full rounds each hour except for the last hour where you played 3 full rounds.

    Constraints:

    • startTime and finishTime are in the format HH:MM.
    • 00 <= HH <= 23
    • 00 <= MM <= 59
    • startTime and finishTime are not equal.

    你完成的完整对局数。

    一款新的在线电子游戏在近期发布,在该电子游戏中,以 刻钟 为周期规划若干时长为 15 分钟 的游戏对局。这意味着,在 HH:00、HH:15、HH:30 和 HH:45 ,将会开始一个新的对局,其中 HH 用一个从 00 到 23 的整数表示。游戏中使用 24 小时制的时钟 ,所以一天中最早的时间是 00:00 ,最晚的时间是 23:59 。

    给你两个字符串 startTime 和 finishTime ,均符合 "HH:MM" 格式,分别表示你 进入 和 退出 游戏的确切时间,请计算在整个游戏会话期间,你完成的 完整对局的对局数 。

    例如,如果 startTime = "05:20" 且 finishTime = "05:59" ,这意味着你仅仅完成从 05:30 到 05:45 这一个完整对局。而你没有完成从 05:15 到 05:30 的完整对局,因为你是在对局开始后进入的游戏;同时,你也没有完成从 05:45 到 06:00 的完整对局,因为你是在对局结束前退出的游戏。
    如果 finishTime 早于 startTime ,这表示你玩了个通宵(也就是从 startTime 到午夜,再从午夜到 finishTime)。

    假设你是从 startTime 进入游戏,并在 finishTime 退出游戏,请计算并返回你完成的 完整对局的对局数 。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/the-number-of-full-rounds-you-have-played
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这是一道字符串/数学题,算是实现题。因为题干中不可能给出所有的 test case 所以这道题我做了很久。虽然最后通过了,但是代码写的不是很精炼。

    我一开始的思路是,如果开始时间和结束时间在同一天,也就是结束时间严格大于开始时间的话,那么我们就正常计算;但是如果开始时间大于结束时间,就说明跨过午夜了,此时我们需要把结束时间 + 24小时这样可以方便计算。接着我们去计算两个时间的差距,事实证明是很麻烦的。比如我的开始时间是47分,不能从45分开始对吧,那么我可能需要将这个分钟数前进到下一个可行的分钟00,但是此时我的小时需要 + 1,但是这个事情只适用于分钟数大于 45 的情况;分钟数小于 45 的 case,小时数不变。

    所以我换了一种思路,与其是直接计算可行的对局数量,不如找一个标准时间,分别去比较 标准时间 - 开始时间 这一段能有多少对局,标准时间 - 结束时间 这一段能有多少对局,然后两者相减即可。我找的标准时间是 00:00 所以我就直接计算从 00:00 到开始时间和从 00:00 到结束时间分别能有多少对局。这里有一个 corner case 是当我们把开始时间折算成分钟 startTotalMins 之后,如果 startTotalMins 不能被 15 整除,我们需要对 startRes + 1,意思是开始时间会多占用一个对局的时间,这样结果里就会少一个对局。另外一个 corner case 是如果开始时间和结束时间的差距在一个小时范围内,尤其是差距在15分钟内的时候,是不能完成对局的,此时要对结果设置一个下限 0。

    时间O(1) - 时间计算是固定的,不会因着 input 变化而变化

    空间O(1)

    Java实现

     1 class Solution {
     2     public int numberOfRounds(String startTime, String finishTime) {
     3         int startHour = Integer.parseInt(startTime.substring(0, startTime.indexOf(':')));
     4         int startMinute = Integer.parseInt(startTime.substring(startTime.indexOf(':') + 1));
     5         int finishHour = Integer.parseInt(finishTime.substring(0, finishTime.indexOf(':')));
     6         int finishMinute = Integer.parseInt(finishTime.substring(finishTime.indexOf(':') + 1));
     7         // System.out.println(startHour + ":" + startMinute + ", " + finishHour + ":" + finishMinute);
     8 
     9         // corner case
    10         if (startHour > finishHour || (startHour == finishHour && startMinute > finishMinute)) {
    11             finishHour += 24;
    12         }
    13         int startRes = 0;
    14         int startTotalMins = startHour * 60 + startMinute;
    15         if (startTotalMins % 15 != 0) {
    16             startRes = startTotalMins / 15 + 1;
    17         } else {
    18             startRes = startTotalMins / 15;
    19         }
    20 
    21         int finishRes = 0;
    22         int finishTotalMins = finishHour * 60 + finishMinute;
    23         finishRes = finishTotalMins / 15;
    24         return Math.max(finishRes - startRes, 0);
    25     }
    26 }

    LeetCode 题目总结

  • 相关阅读:
    MapReduce-shuffle过程详解
    YARN中的失败分析
    HBase协处理器的使用(添加Solr二级索引)
    Flume具体应用(多案例)
    Flume架构及运行机制
    python Cmd实例之网络爬虫应用
    mongodb3 权限认证问题总结
    webpack配置
    apt软件包管理
    python笔记之编程风格大比拼
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14925111.html
Copyright © 2011-2022 走看看