HomeGorakh Raj Joshi

Fibonacci Number

Time Complexity O(n)/Space Complexity O(1)

  • #LeetCode

https://leetcode.com/problems/fibonacci-number/

var fib = function (n) {
  if (n < 1) return 0;
  if (n < 3) return 1;

  let a = 1;
  let b = 1;
  let c = 0;

  for (i = 3; i <= n; i++) {
    c = a + b;
    a = b;
    b = c;
  }

  return c;
};

Gorakh Raj Joshi

Senior Fullstack Engineer: Specializing in System Design and Architecture, Accessibility, and Frontend Interface Design

LinkedIn

GitHub

Email

All rights reserved © Gorakh Raj Joshi 2024