Ctrl K

Fewest Fibonacci Terms

Medium
The Fibonacci numbers start with two 1s, and every next one is the sum of the previous two: 1, 1, 2, 3, 5, 8, 13, … You are given an integer total. Write it as a sum of Fibonacci numbers using as few terms as possible and return how many terms you used. The same Fibonacci number may appear more than once. Such a sum exists for every positive total.
Example 1
total=12 → 3

The Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, … We get 12 = 8 + 3 + 1, and one or two terms are not enough.

Example 2
total=16 → 2

16 = 13 + 3, so two terms suffice.

Example 3
total=21 → 1

21 is itself a Fibonacci number.

Constraints
  • 1 ≤ total ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSmallest Number From Digit Factors MediumOne Digit Swap MediumLargest Number With Non-Decreasing Digits MediumFewest Guests by Lanyard Color MediumUp or Down for Every Post Medium
loading Python…
Input
total=12
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 68 tests on the server, hidden ones included.