{"id":6309,"date":"2020-02-13T22:36:37","date_gmt":"2020-02-14T06:36:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6309"},"modified":"2020-02-13T22:38:02","modified_gmt":"2020-02-14T06:38:02","slug":"leetcode-1344-angle-between-hands-of-a-clock","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1344-angle-between-hands-of-a-clock\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1344. Angle Between Hands of a Clock"},"content":{"rendered":"\n<p>Given two numbers,&nbsp;<code>hour<\/code>&nbsp;and&nbsp;<code>minutes<\/code>. Return the smaller angle (in sexagesimal units) formed between the&nbsp;<code>hour<\/code>&nbsp;and the&nbsp;<code>minute<\/code>&nbsp;hand.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/12\/26\/sample_1_1673.png\" alt=\"\" width=\"184\" height=\"181\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> hour = 12, minutes = 30\n<strong>Output:<\/strong> 165\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/12\/26\/sample_2_1673.png\" alt=\"\" width=\"181\" height=\"181\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> hour = 3, minutes = 30\n<strong>Output:<\/strong> 75\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/12\/26\/sample_3_1673.png\" alt=\"\" width=\"181\" height=\"181\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> hour = 3, minutes = 15\n<strong>Output:<\/strong> 7.5\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> hour = 4, minutes = 50\n<strong>Output:<\/strong> 155\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> hour = 12, minutes = 0\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= hour &lt;= 12<\/code><\/li><li><code>0 &lt;= minutes &lt;= 59<\/code><\/li><li>Answers within&nbsp;<code>10^-5<\/code>&nbsp;of the actual value will be accepted as correct.<\/li><\/ul>\n\n\n\n<p>Solution: Math<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Compute the angle of the hour hand (h  + m \/ 60.0) * 360 \/ 12 as a_h<\/li><li>Compute the angle of the minute hand m \/ 60.0 * 360 as a_m<\/li><li>ans = min(abs(a_h &#8211; a_m), 360 &#8211; abs(a_h &#8211; a_m))<\/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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  double angleClock(int hour, int minutes) {\n    double a_m = minutes * 360 \/ 60;\n    double a_h = (hour + minutes \/ 60.0) * 360 \/ 12;\n    return min(abs(a_m - a_h), 360 - abs(a_m - a_h));\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two numbers,&nbsp;hour&nbsp;and&nbsp;minutes. Return the smaller angle (in sexagesimal units) formed between the&nbsp;hour&nbsp;and the&nbsp;minute&nbsp;hand. Example 1: Input: hour = 12, minutes = 30 Output: 165&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[551,108,31,177],"class_list":["post-6309","post","type-post","status-publish","format-standard","hentry","category-math","tag-angle","tag-clock","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6309","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=6309"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6309\/revisions"}],"predecessor-version":[{"id":6312,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6309\/revisions\/6312"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}