Problem · Math
Minimum Steps to a Fibonacci Number
Learn this problemProblem statement
Given an integer x, return the minimum number of steps required to change x into a Fibonacci number.
In each step, you may either increment or decrement the current number by 1.
The Fibonacci sequence is defined as follows:
F[0] = 0F[1] = 1- For every
i >= 2,F[i] = F[i - 1] + F[i - 2].
The elements of this sequence are called Fibonacci numbers.
Function
minimumFibonacciSteps(x: int) → intExamples
Example 1
x = 15return = 2The closest Fibonacci number is 13. Decrementing 15 twice reaches 13, so the minimum number of steps is 2.
Example 2
x = 1return = 01 is already a Fibonacci number, so no steps are needed.
Example 3
x = 13return = 013 is already a Fibonacci number, so no steps are needed.
Constraints
0 <= x <= 1,000,000