{"id":8584,"date":"2021-08-15T12:20:36","date_gmt":"2021-08-15T19:20:36","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8584"},"modified":"2021-08-15T12:22:07","modified_gmt":"2021-08-15T19:22:07","slug":"leetcode-1904-the-number-of-full-rounds-you-have-played","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1904-the-number-of-full-rounds-you-have-played\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1904. The Number of Full Rounds You Have Played"},"content":{"rendered":"\n<p>A new online video game has been released, and in this video game, there are&nbsp;<strong>15-minute<\/strong>&nbsp;rounds scheduled every&nbsp;<strong>quarter-hour<\/strong>&nbsp;period. This means that at&nbsp;<code>HH:00<\/code>,&nbsp;<code>HH:15<\/code>,&nbsp;<code>HH:30<\/code>&nbsp;and&nbsp;<code>HH:45<\/code>, a new round starts, where&nbsp;<code>HH<\/code>&nbsp;represents an integer number from&nbsp;<code>00<\/code>&nbsp;to&nbsp;<code>23<\/code>. A&nbsp;<strong>24-hour clock<\/strong>&nbsp;is used, so the earliest time in the day is&nbsp;<code>00:00<\/code>&nbsp;and the latest is&nbsp;<code>23:59<\/code>.<\/p>\n\n\n\n<p>Given two strings&nbsp;<code>startTime<\/code>&nbsp;and&nbsp;<code>finishTime<\/code>&nbsp;in the format&nbsp;<code>\"HH:MM\"<\/code>&nbsp;representing the exact time you&nbsp;<strong>started<\/strong>&nbsp;and&nbsp;<strong>finished<\/strong>&nbsp;playing the game, respectively, calculate the&nbsp;<strong>number of full rounds<\/strong>&nbsp;that you played during your game session.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, if&nbsp;<code>startTime = \"05:20\"<\/code>&nbsp;and&nbsp;<code>finishTime = \"05:59\"<\/code>&nbsp;this means you played only one full round from&nbsp;<code>05:30<\/code>&nbsp;to&nbsp;<code>05:45<\/code>. You did not play the full round from&nbsp;<code>05:15<\/code>&nbsp;to&nbsp;<code>05:30<\/code>&nbsp;because you started after the round began, and you did not play the full round from&nbsp;<code>05:45<\/code>&nbsp;to&nbsp;<code>06:00<\/code>&nbsp;because you stopped before the round ended.<\/li><\/ul>\n\n\n\n<p>If&nbsp;<code>finishTime<\/code>&nbsp;is&nbsp;<strong>earlier<\/strong>&nbsp;than&nbsp;<code>startTime<\/code>, this means you have played overnight (from&nbsp;<code>startTime<\/code>&nbsp;to the midnight and from midnight to&nbsp;<code>finishTime<\/code>).<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>number of full rounds<\/strong>&nbsp;that you have played if you had started playing at&nbsp;<\/em><code>startTime<\/code><em>&nbsp;and finished at&nbsp;<\/em><code>finishTime<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> startTime = \"12:01\", finishTime = \"12:44\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> You played one full round from 12:15 to 12:30.\nYou did not play the full round from 12:00 to 12:15 because you started playing at 12:01 after it began.\nYou did not play the full round from 12:30 to 12:45 because you stopped playing at 12:44 before it ended.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> startTime = \"20:00\", finishTime = \"06:00\"\n<strong>Output:<\/strong> 40\n<strong>Explanation:<\/strong> You played 16 full rounds from 20:00 to 00:00 and 24 full rounds from 00:00 to 06:00.\n16 + 24 = 40.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> startTime = \"00:00\", finishTime = \"23:59\"\n<strong>Output:<\/strong> 95\n<strong>Explanation:<\/strong> You played 4 full rounds each hour except for the last hour where you played 3 full rounds.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>startTime<\/code>&nbsp;and&nbsp;<code>finishTime<\/code>&nbsp;are in the format&nbsp;<code>HH:MM<\/code>.<\/li><li><code>00 &lt;= HH &lt;= 23<\/code><\/li><li><code>00 &lt;= MM &lt;= 59<\/code><\/li><li><code>startTime<\/code>&nbsp;and&nbsp;<code>finishTime<\/code>&nbsp;are not equal.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: String \/ Simple math<\/strong><\/h2>\n\n\n\n<p>ans = max(0, floor(end \/ 15) &#8211; ceil(start \/ 15))<\/p>\n\n\n\n<p>Tips: <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Write a reusable function to parse time to minutes.<\/li><li>a \/ b for floor, (a + b &#8211; 1) \/ b for ceil<\/li><\/ol>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int numberOfRounds(string startTime, string finishTime) {\n    auto parseTime = [](string t) {\n      return ((t[0] - '0') * 10 + (t[1] - '0')) * 60 + (t[3] - '0') * 10 + t[4] - '0';\n    };\n    int m1 = parseTime(startTime);\n    int m2 = parseTime(finishTime);\n    if (m2 < m1) m2 += 24 * 60;\n    return max(0, m2 \/ 15 - (m1 + 14) \/ 15);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A new online video game has been released, and in this video game, there are&nbsp;15-minute&nbsp;rounds scheduled every&nbsp;quarter-hour&nbsp;period. This means that at&nbsp;HH:00,&nbsp;HH:15,&nbsp;HH:30&nbsp;and&nbsp;HH:45, a new round starts,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[108,31,177,4,107],"class_list":["post-8584","post","type-post","status-publish","format-standard","hentry","category-string","tag-clock","tag-math","tag-medium","tag-string","tag-time","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8584"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8584\/revisions"}],"predecessor-version":[{"id":8586,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8584\/revisions\/8586"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}