Lintcode Fibonacci

Find the Nth number in Fibonacci sequence.

A Fibonacci sequence is defined as follow:

  • The first two numbers are 0 and 1.
  • The i th number is the sum of i-1 th number and i-2 th number.

    The first ten numbers in Fibonacci sequence is:

    0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...

    Example

    Given 1, return 0

    Given 2, return 1

    Given 10, return 34

    Note

    The Nth fibonacci number won‘t exceed the max value of signed 32-bit integer in the test cases.

    In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

        Fn = Fn-1 + Fn-2

    with seed values

       F1 = 0 and F2 = 1

    Method 1 ( Use recursion )

    int fibonacci(int n) {
          if (n == 1)
          return 0;
          else if (n == 2)
          return 1;
          return fib(n-1) + fib(n-2);
    }

    Time Complexity: T(n) = T(n-1) + T(n-2) which is exponential. So this is a bad implementation for nth Fibonacci number.

    Extra Space: O(n) if we consider the function call stack size, otherwise O(1).

    time limited

    Method 2 ( Use Dynamic Programming )

    int fibonacci(int n)
    {
    /* Declare an array to store Fibonacci numbers. */
      int f[n+1];
      int i;
      /* 0th and 1st number of the series are 0 and 1*/
      f[1] = 0;
      f[2] = 1;
      for (i = 2; i <= n; i++)
      {
          /* Add the previous 2 numbers in the series and store it */
          f[i] = f[i-1] + f[i-2];
      }
      return f[n];
    }

    Time Complexity: O(n)
    Extra Space: O(n)

    12~14ms

    Method 3 ( Space Otimized Method 2)

    We can optimize the space used in method 2 by storing the previous two numbers only because that is all we need to get the next Fibannaci number in series.

    int fibonacci(int n)
    {
      int a = 0, b = 1, c, i;
      if( n == 1) return a; 
      if( n == 2) return b; 
      for (i = 2; i <= n; i++)
      {
         c = a + b;
         a = b;
         b = c;
      }
      return b;
    }

    10ms

    Time Complexity: O(n)
    Extra Space: O(1)

    时间: 2024-10-11 22:59:36

    Lintcode Fibonacci的相关文章

    LintCode:Fibonacci

    C++ 1 class Solution{ 2 public: 3 /** 4 * @param n: an integer 5 * @return an integer f(n) 6 */ 7 int fibonacci(int n) { 8 // write your code here 9 int a=0,b=1; 10 for (int i=1; i<n; i++) { 11 a = a+b; 12 b = a-b; 13 } 14 return a; 15 } 16 };

    LintCode 366 Fibonacci

    /* 1st method will lead to time limit *//* the time complexity is exponential sicne T(n) = T(n-1) + T(n-2) */ class Solution { /** * @param n: an integer * @return an integer f(n) */ public int fibonacci(int n) { // write your code here if (n == 1 ||

    LintCode:剑指Offer

    第1章: 9.Fizz Buzz :http://www.lintcode.com/zh-cn/problem/fizz-buzz/ 解法1:(%  String.valueOf) (1.rst;  2.for(1,n),15,5,3,else;  3.return) 1 public class Solution { 2 public List<String> fizzBuzz(int n) { 3 List<String> rst = new ArrayList<>

    lintcode bugfree and good codestyle note

    2016.12.4, 366 http://www.lintcode.com/en/problem/fibonacci/ 一刷使用递归算法,超时.二刷使用九章算术的算法,就是滚动指针的思路,以前写python的时候也玩过,但是给忘了,这次又用c++拾起来了.lint有bug,不能用,很烦. class Solution { public: /** * @param n: an integer * @return an integer f(n) */ int fibonacci(int n) {

    LintCode Python 入门级题目 斐波纳契数列

    原题描述: 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ... 题目分析: 开始的想法,通过递归实现输出fib(n-1)+fib(n-2)的值计算,原理正确,算法复杂度高,导致运行时间超过lintcode限制: class Solution: # @param n: an integer # @retur

    LintCode题解之斐波纳契数列

    直接使用递归的方法会导致TLE,加个缓存就好了: public class Solution { private Integer[] buff = new Integer[1000]; /* * @param n: an integer * @return: an ineger f(n) */ public int fibonacci(int n) { if(buff[n]!=null) return buff[n]; else if(n==1) return buff[1] = 0; else

    NYOJ 480 Fibonacci Again!

    Fibonacci Again! 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 求第n个斐波那契数是否是一个素数,n为整数 f[n]=f[n-1]+f[n-2] (2<n<30) f[1]=3,f[2]=7 输入 输入整数m,0<m<30,输入-1表示结束输入 输出 假设f[m]是素数 则输出Yes,否则输出No, 每行输出占一行. 例子输入 2 3 例子输出 Yes No #include<stdio.h> int f[35]={0

    HDU1848 Fibonacci again and again

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8198    Accepted Submission(s): 3412 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;

    [lintcode the-smallest-difference]最小差(python)

    题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回最小差. 排好序后用两个指针分别扫描两个数组,每次更新他们的差值的绝对值.并且依据他们两个数字的大小来决定谁来移动指针. 1 class Solution: 2 # @param