{"id":5073,"date":"2019-04-17T09:15:12","date_gmt":"2019-04-17T16:15:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5073"},"modified":"2019-04-17T09:18:04","modified_gmt":"2019-04-17T16:18:04","slug":"leetcode-279-perfect-squares","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-279-perfect-squares\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 279. Perfect Squares"},"content":{"rendered":"\n<p>Given a positive integer&nbsp;<em>n<\/em>, find the least number of perfect square numbers (for example,&nbsp;<code>1, 4, 9, 16, ...<\/code>) which sum to&nbsp;<em>n<\/em>.<\/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> <em>n<\/em> = <code>12<\/code>\n<strong>Output:<\/strong> 3 \n<strong>Explanation: <\/strong><code>12 = 4 + 4 + 4.<\/code><\/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> <em>n<\/em> = <code>13<\/code>\n<strong>Output:<\/strong> 2\n<strong>Explanation: <\/strong><code>13 = 4 + 9.<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: DP<\/strong><\/h2>\n\n\n\n<p>dp[i] := ans<br>dp[0] = 0<br>dp[i] = min{dp[i &#8211; j * j] + 1}  1 &lt;= j * j &lt;= i<\/p>\n\n\n\n<p>dp[5] = min{<br>  dp[5 &#8211; 2 * 2] + 1 = dp[1] + 1 = (dp[1 &#8211; 1 * 1] + 1) + 1 = dp[0] + 1 + 1 = 2,<br>  dp[5 &#8211; 1 * 1] + 1 = dp[3] + 1 = (dp[3 &#8211; 1 * 1] + 1) + 1 = dp[1] + 2 = dp[1 &#8211; 1*1] + 1 + 2 = dp[0] + 3 = 3<br>};<\/p>\n\n\n\n<p>dp[5] = 2<br><\/p>\n\n\n\n<p>Time complexity: O(n * sqrt(n))<br>Space complexity: O(n)<\/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, running time: 104 ms\nclass Solution {\npublic:\n  int numSquares(int n) {\n    vector<int> dp(n + 1, INT_MAX >> 1);\n    dp[0] = 0;\n    for (int i = 1; i <= n; ++i)\n      for (int j = 1; j * j <= i; ++j) \n        dp[i] = min(dp[i], dp[i - j * j] + 1);\n    return dp[n];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a positive integer&nbsp;n, find the least number of perfect square numbers (for example,&nbsp;1, 4, 9, 16, &#8230;) which sum to&nbsp;n. Example 1: Input: n&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,31,177],"class_list":["post-5073","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5073","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=5073"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5073\/revisions"}],"predecessor-version":[{"id":5077,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5073\/revisions\/5077"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}