{"id":2099,"date":"2016-07-31T21:09:14","date_gmt":"2016-07-31T13:09:14","guid":{"rendered":"http:\/\/boweihe.me\/?p=2099"},"modified":"2016-07-31T21:09:14","modified_gmt":"2016-07-31T13:09:14","slug":"leetcode-343-integer-break","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2099","title":{"rendered":"LeetCode 343. Integer Break"},"content":{"rendered":"<p>Given a positive integer <i>n<\/i>, break it into the sum of <b>at least<\/b> two positive integers and maximize the product of those integers. Return the maximum product you can get.<br \/>\nFor example, given <i>n<\/i> = 2, return 1 (2 = 1 + 1); given <i>n<\/i> = 10, return 36 (10 = 3 + 3 + 4).<br \/>\n<b>Note<\/b>: You may assume that <i>n<\/i> is not less than 2 and not larger than 58.<\/p>\n<h3>\u601d\u8def O(n^2)<\/h3>\n<p><em>\u6ce8\uff1a\u8fd9\u9898\u8fd8\u6709O(n)\u89e3\u51b3\u7684\u65b9\u6cd5\uff0c\u6211\u63d0\u4ea4\u7684\u65b9\u6cd5\u662fO(n^2)\u7684\u3002<\/em><br \/>\n\u6709\u70b9\u50cf\u52a8\u6001\u89c4\u5212\u7684\u9898\uff0c\u4e0a\u4e00\u4e2a\u6570\u7684\u72b6\u6001\u7ecf\u8fc7\u4e58\u6cd5\u53ef\u4ee5\u4f5c\u4e3a\u4e0b\u4e2a\u6570\u7684\u5907\u9009\uff08\u8981\u4e0e\u4e0b\u4e2a\u6570\u6bd4\u8f83\uff09\u3002<br \/>\n\u5047\u8bberesult[i]\u5b58\u4e86\u5f53\u524d\u80fd\u52a0\u5230i\u7684\u6700\u597d\u7ed3\u679c\u503c\uff1b<br \/>\n\u7b2c\u4e00\u904d\uff0c\u4ece1\u5f00\u59cb\u5f80\u540e\u52a0(\u53cd\u6b63\u6700\u5927\u7684n\u4e5f\u5c3158)\uff0c1+1=2\uff0c\u5219result[2]\u4e2d\u5c31\u662f1*1=1\uff0c1+2=3=&gt;result[3]=1*2=2\uff1bresult[4]=3<br \/>\n\u7b2c\u4e8c\u904d\uff0c\u4ece2\u5f00\u59cb\u5f80\u540e\u52a0\uff0ca[3]=2\uff0ca[4]=2*2(\u539f\u59cb\u503c\u4e3a3\uff0c\u8fd9\u91cc\u66f4\u65b0\u4e3a4),&#8230;<br \/>\n&#8230;<br \/>\n\u7b2c\u4e94\u904d\uff0c\u4ece5\u5f00\u59cb\u5f80\u540e\u52a0\uff0c\u4f46\u4e4b\u524d\u7b97\u7684\u8fc7\u7a0b\u4e2d\uff0cresult[5]\u7684\u7ed3\u679c\u5df2\u7ecf\u88ab\u66f4\u65b0\u6210\u4e866\uff0c\u8fd9\u4e2a\u6570\u6bd45\u5927\uff0c\u56e0\u6b64\u00d7\u7684\u65f6\u5019\uff0c\u88ab\u4e58\u6570\u662f6\u800c\u4e0d\u662f5\uff1b<br \/>\n\u4ee5\u6b64\u7c7b\u63a8&#8230;<\/p>\n<h3>\u601d\u8def O(logn)<\/h3>\n<p>\u9700\u8981\u4e00\u70b9\u6570\u5b66\u601d\u7ef4\uff0c\u8be6\u89c1<a href=\"https:\/\/discuss.leetcode.com\/topic\/42991\/o-log-n-time-solution-with-explanation\">https:\/\/discuss.leetcode.com\/topic\/42991\/o-log-n-time-solution-with-explanation<\/a><\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:java decode:true \">public class Solution {\n    public int max(int x, int y){\n        return x&gt;y ? x : y;\n    }\n    public int integerBreak(int n) {\n        int[] result = new int[59]; \/\/0 to 58\n        result[1] = 1;  \/\/dummy\n        for(int i=1; i&lt;=n-1; i++)\n        {\n            for(int j=1; j&lt;=n-i; j++)\n            {\n                int temp = result[i];\n                if(result[i] &lt; i)\n                    temp = i;\n                result[i+j]=max(result[i+j], temp * j);\n            }\n        }\n        return result[n];\n    }\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-2099","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2099"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2099\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}