{"id":2291,"date":"2018-03-22T08:44:46","date_gmt":"2018-03-22T15:44:46","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2291"},"modified":"2018-03-22T08:44:57","modified_gmt":"2018-03-22T15:44:57","slug":"leetcode-553-optimal-division","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-553-optimal-division\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 553. Optimal Division"},"content":{"rendered":"<h1>Problem<\/h1>\n<p><a href=\"https:\/\/leetcode.com\/problems\/optimal-division\/description\/\">https:\/\/leetcode.com\/problems\/optimal-division\/description\/<\/a><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u8fde\u9664\u7684\u8868\u8fbe\u5f0f\uff0c\u8ba9\u4f60\u52a0\u4e00\u4e9b\u62ec\u53f7\u4f7f\u5f97\u8868\u8fbe\u5f0f\u7684\u503c\u6700\u5927\u3002<\/p>\n<p>Given a list of\u00a0<b>positive integers<\/b>, the adjacent integers will perform the float division. For example, [2,3,4] -&gt; 2 \/ 3 \/ 4.<\/p>\n<p>However, you can add any number of parenthesis at any position to change the priority of operations. You should find out how to add parenthesis to get the\u00a0<b>maximum<\/b>\u00a0result, and return the corresponding expression in string format.\u00a0<b>Your expression should NOT contain redundant parenthesis.<\/b><\/p>\n<h2><b>Example:<\/b><\/h2>\n<pre class=\"crayon:false\"><b>Input:<\/b> [1000,100,10,2]\r\n<b>Output:<\/b> \"1000\/(100\/10\/2)\"\r\n<b>Explanation:<\/b>\r\n1000\/(100\/10\/2) = 1000\/((100\/10)\/2) = 200\r\nHowever, the bold parenthesis in \"1000\/(<b>(<\/b>100\/10<b>)<\/b>\/2)\" are redundant, \r\nsince they don't influence the operation priority. So you should return \"1000\/(100\/10\/2)\". \r\n\r\nOther cases:\r\n1000\/(100\/10)\/2 = 50\r\n1000\/(100\/(10\/2)) = 50\r\n1000\/100\/10\/2 = 0.5\r\n1000\/100\/(10\/2) = 2\r\n<\/pre>\n<h2><b>Note:<\/b><\/h2>\n<ol>\n<li>The length of the input array is [1, 10].<\/li>\n<li>Elements in the given array will be in range [2, 1000].<\/li>\n<li>There is only one optimal division for each test case.<\/li>\n<\/ol>\n<h1>Solution: Math<\/h1>\n<p>For: X1\/X2\/X3\/&#8230;\/Xn, X1\/(X2\/X3\/&#8230;\/Xn) &gt; X1\/X2\/(X3\/&#8230;\/Xn) &gt; X1\/X2\/X3\/(&#8230;\/Xn)<\/p>\n<p>X1\/(X2\/X3\/&#8230;\/Xn) is the max value.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 5 ms\r\nclass Solution {\r\npublic:\r\n  string optimalDivision(vector&lt;int&gt;&amp; nums) {\r\n    string ans = to_string(nums[0]);\r\n    if (nums.size() == 1) return ans;\r\n    else if (nums.size() == 2) return ans + \"\/\" + to_string(nums[1]);\r\n    \r\n    ans += \"\/(\" +  to_string(nums[1]);\r\n    for (int i = 2; i &lt; nums.size(); ++i)\r\n      ans += \"\/\" + to_string(nums[i]);\r\n    ans += \")\";\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem https:\/\/leetcode.com\/problems\/optimal-division\/description\/ \u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u8fde\u9664\u7684\u8868\u8fbe\u5f0f\uff0c\u8ba9\u4f60\u52a0\u4e00\u4e9b\u62ec\u53f7\u4f7f\u5f97\u8868\u8fbe\u5f0f\u7684\u503c\u6700\u5927\u3002 Given a list of\u00a0positive integers, the adjacent integers will perform the float division. For example, [2,3,4] -&gt; 2 \/ 3 \/ 4.&#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,47],"tags":[266,31,177],"class_list":["post-2291","post","type-post","status-publish","format-standard","hentry","category-math","category-string","tag-division","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2291","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=2291"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2291\/revisions"}],"predecessor-version":[{"id":2293,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2291\/revisions\/2293"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}