{"id":6632,"date":"2020-04-18T11:31:10","date_gmt":"2020-04-18T18:31:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6632"},"modified":"2020-04-18T11:32:12","modified_gmt":"2020-04-18T18:32:12","slug":"leetcode-1414-find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1414-find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1414. Find the Minimum Number of Fibonacci Numbers Whose Sum Is K"},"content":{"rendered":"\n<p>Given the number&nbsp;<code>k<\/code>,&nbsp;<em>return the minimum number of Fibonacci numbers whose sum is equal to&nbsp;<\/em><code>k<\/code>, whether a Fibonacci number could be used multiple times.<\/p>\n\n\n\n<p>The Fibonacci numbers are defined as:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>F<sub>1<\/sub>&nbsp;= 1<\/li><li>F<sub>2<\/sub>&nbsp;= 1<\/li><li>F<sub>n<\/sub>&nbsp;= F<sub>n-1<\/sub>&nbsp;+ F<sub>n-2<\/sub>&nbsp;, for n &gt; 2.<\/li><\/ul>\n\n\n\n<p>It is guaranteed that for the given constraints we can always find such fibonacci numbers that sum&nbsp;<code>k<\/code>.<\/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> k = 7\n<strong>Output:<\/strong> 2 \n<strong>Explanation:<\/strong> The Fibonacci numbers are: 1, 1, 2, 3, 5, 8, 13, ... \nFor k = 7 we can use 2 + 5 = 7.<\/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> k = 10\n<strong>Output:<\/strong> 2 \n<strong>Explanation:<\/strong> For k = 10 we can use 2 + 8 = 10.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> k = 19\n<strong>Output:<\/strong> 3 \n<strong>Explanation:<\/strong> For k = 19 we can use 1 + 5 + 13 = 19.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= k &lt;= 10^9<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Find the largest fibonacci numbers x that x &lt;= k, ans = 1 + find(k &#8211; x)<\/p>\n\n\n\n<p>Time complexity: O(logk^2) -&gt; O(logk)<br>Space complexity: O(logk) -&gt; O(1)<\/p>\n\n\n\n<p><strong>Recursive<\/strong><\/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\nclass Solution {\npublic:\n  int findMinFibonacciNumbers(int k) {\n    if (k == 0) return 0;\n    int f1 = 1;\n    int f2 = 1;    \n    while (f2 <= k) {\n      swap(f1, f2);\n      f2 += f1;\n    }    \n    return 1 + findMinFibonacciNumbers(k - f1);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><strong>Iterative<\/strong><\/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\nclass Solution {\npublic:\n  int findMinFibonacciNumbers(int k) {\n    int f1 = 1;\n    int f2 = 1;\n    int ans = 1;\n    while (f2 <= k) {\n      swap(f1, f2);\n      f2 += f1;\n    }    \n    k -= f1;\n    while (k) {\n      if (k >= f1) {\n        k -= f1;\n        ans += 1;\n      }\n      f2 -= f1;\n      swap(f1, f2);      \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the number&nbsp;k,&nbsp;return the minimum number of Fibonacci numbers whose sum is equal to&nbsp;k, whether a Fibonacci number could be used multiple times. The Fibonacci&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,17],"class_list":["post-6632","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-recursion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6632","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=6632"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6632\/revisions"}],"predecessor-version":[{"id":6634,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6632\/revisions\/6634"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}