{"id":2183,"date":"2018-03-17T22:05:45","date_gmt":"2018-03-18T05:05:45","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2183"},"modified":"2018-03-17T23:00:31","modified_gmt":"2018-03-18T06:00:31","slug":"leetcode-simple-rgb-color","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-simple-rgb-color\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 800. Simple RGB Color"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>In the following, every capital letter represents some hexadecimal digit from\u00a0<code>0<\/code>\u00a0to\u00a0<code>f<\/code>.<\/p>\n<p>The red-green-blue color\u00a0<code>\"#AABBCC\"<\/code>\u00a0can be written\u00a0as\u00a0<code>\"#ABC\"<\/code>\u00a0in\u00a0shorthand.\u00a0 For example,\u00a0<code>\"#15c\"<\/code>\u00a0is shorthand for the color\u00a0<code>\"#1155cc\"<\/code>.<\/p>\n<p>Now, say the similarity between two colors\u00a0<code>\"#ABCDEF\"<\/code>\u00a0and\u00a0<code>\"#UVWXYZ\"<\/code>\u00a0is\u00a0<code>-(AB - UV)^2 -\u00a0(CD - WX)^2 -\u00a0(EF - YZ)^2<\/code>.<\/p>\n<p>Given the color\u00a0<code>\"#ABCDEF\"<\/code>, return a 7 character color\u00a0that is most similar to\u00a0<code>#ABCDEF<\/code>, and has a shorthand (that is, it can be represented as some\u00a0<code>\"#XYZ\"<\/code><\/p>\n<pre class=\"crayon:false \"><strong>Example 1:<\/strong>\r\n<strong>Input:<\/strong> color = \"#09f166\"\r\n<strong>Output:<\/strong> \"#11ee66\"\r\n<strong>Explanation: <\/strong> \r\nThe similarity is -(0x09 - 0x11)^2 -(0xf1 - 0xee)^2 - (0x66 - 0x66)^2 = -64 -9 -0 = -73.\r\nThis is the highest among any shorthand color.\r\n<\/pre>\n<h2><strong>Note:<\/strong><\/h2>\n<ul>\n<li><code>color<\/code>\u00a0is a string of length\u00a0<code>7<\/code>.<\/li>\n<li><code>color<\/code>\u00a0is a valid RGB color: for\u00a0<code>i &gt; 0<\/code>,\u00a0<code>color[i]<\/code>\u00a0is a hexadecimal digit from\u00a0<code>0<\/code>\u00a0to\u00a0<code>f<\/code><\/li>\n<li>Any answer which has the same (highest)\u00a0similarity as the best answer will be accepted.<\/li>\n<li>All inputs and outputs should use lowercase letters, and the output is 7 characters.<\/li>\n<\/ul>\n<h1><strong>Solution: Brute Force<\/strong><\/h1>\n<p>R, G, B are independent, find the closest color for each channel separately.<\/p>\n<p>Time complexity: O(3 * 16)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 7 ms\r\nclass Solution {\r\npublic:\r\n  string similarRGB(string color) {\r\n    const string hex{\"0123456789abcdef\"};\r\n    vector&lt;int&gt; rgb(3, 0);\r\n    for (int i = 0; i &lt; 3; ++i)\r\n      rgb[i] = hex.find(color[2 * i + 1]) * 16 + hex.find(color[2 * i + 2]);\r\n    \r\n    string ans(7, '#');    \r\n    for (int i = 0; i &lt; 3; ++i) {\r\n      int best = INT_MAX;\r\n      for (int j = 0; j &lt; 16; ++j) {\r\n        int diff = abs(j * 16 + j - rgb[i]);\r\n        if (diff &gt;= best) continue;\r\n        best = diff;\r\n        ans[2 * i + 1] = ans[2 * i + 2] = hex[j];\r\n      }\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem In the following, every capital letter represents some hexadecimal digit from\u00a00\u00a0to\u00a0f. The red-green-blue color\u00a0&#8220;#AABBCC&#8221;\u00a0can be written\u00a0as\u00a0&#8220;#ABC&#8221;\u00a0in\u00a0shorthand.\u00a0 For example,\u00a0&#8220;#15c&#8221;\u00a0is shorthand for the color\u00a0&#8220;#1155cc&#8221;. Now, say&#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":[93,222,10,261],"class_list":["post-2183","post","type-post","status-publish","format-standard","hentry","category-string","tag-conversion","tag-easy","tag-hex","tag-rgb","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2183","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=2183"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2183\/revisions"}],"predecessor-version":[{"id":2193,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2183\/revisions\/2193"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}