Leetcode-Fibonacci sequence (js)

Understanding the Fibonacci sequence

The nth number is obtained by adding the first two of the sequence: f(n) = f(n-1) + f(n -2), using code to implement the Fibonacci sequence is nothing more than investigating the recursive writing However, it is not feasible to use recursion alone to strictly require time complexity and space complexity, because he has done countless useless calculations.

1, 1, 2, 3, 5, 8, 13, …

Ordinary implementation (recursive)

When calculating in this way, there will be a lot of repeated calculations, and the number of recursive layers is getting deeper and deeper, and it is easy to recursively burst the stack.

const fib = n => n <= 1 ? n :fib(n-1)+fib(n-2)
console.log(fib(5))

Reduce time complexity (closure + recursion)

By adding a layer of cache, it is used to store the values ​​that have been calculated before. When a new value needs to be calculated, the cache is searched first, and the cache hits are returned directly, and the calculation is continued after a miss. But the use of arrays increases the space complexity.

const fib3 = function (n) {
    
    
  n <= 1 && n
  const cache = []
  cache[0] = 1
  cache[1] = 1
  function memoize(num) {
    
    
      if (cache[num] !== undefined) {
    
    
          return cache[num]
      }
      cache[num] = memoize(num - 1) + memoize(num - 2)
      // console.log(cache[num])
      return cache[num]
  }
  const result = memoize(n-1)
  return result
}
console.log(fib3(4))

However, there is a problem with such an implementation. If you do not calculate the Fibonacci sequence in sequence, it will increase additional consumption. For example, directly calculate fib(1000). At this time, the other array items in the middle will be initialized to undefined. This will be small. Some time, but after the calculation is completed, the Fibonacci sequence between 1-1000 is filled.
One solution on the reference network is to replace the array with objects for caching, so that the time to fill undefined is less

// 闭包 + 缓存对象
const fibonacci = (function () {
    
    
  const f = {
    
    }
  return function(n) {
    
    
    if(n === 0 || n === 1) {
    
    
      return n
    }
    if(f[n-2] === undefined) {
    
    
      f[n-2] = fibonacci(n-2)
    }
    if(f[n-1] === undefined) {
    
    
      f[n-1] = fibonacci(n-1)
    }
    return f[n] = f[n-1] + f[n-2]
  }
})()

To change the way of writing from front to back::: From the previous count step by step, calculate step by step and calculate until n

const fib =function (n){
    
    
  n <= 1 && n
  const cache = []
  cache[0] = 1
  cache[1] = 1
  for (let i= 2; i<=n; i++ ) {
    
    
    cache[i] = cache[i-1] +cache[i-2]
  }
  return cache[n-1]
}
console.log(fib(5))

Reduce space complexity

Do not use and arrays, reduce the space dimension from front to back

const fib = n =>{
    
    
    n <= 1 && n
    let prev2 = 0
    let prev1 = 1
    let result = 0
    for (let i = 2;i<=n;i++) {
    
    
      result =prev2+prev1
      prev2 = prev1
      prev1 = result
    }
    return result
}

Guess you like

Origin blog.csdn.net/pz1021/article/details/105752392