← Home

509. Fibonacci Number

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
509

Let's make this one less mysterious. For 509. Fibonacci Number, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are fib.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01/**
02The Fibonacci numbers, commonly denoted F(n) form a sequence, 
03called the Fibonacci sequence, 
04such that each number is the sum of the two preceding ones, 
05starting from 0 and 1. That is,
06
07F(0) = 0,   F(1) = 1
08F(N) = F(N - 1) + F(N - 2), for N > 1.
09Given N, calculate F(N).
10**/
11
12//recursive solution
13//Runtime: 16 ms, faster than 32.44% of C++ online submissions for Fibonacci Number.
14//Memory Usage: 8.5 MB, less than 38.31% of C++ online submissions for Fibonacci Number.
15class Solution {
16public:
17    int fib(int N) {
18        if(N==0 or N==1){
19            return N;
20        }else{
21            return fib(N-1) + fib(N-2);
22        }
23    }
24};
25
26//dynamic programming
27//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Fibonacci Number.
28//Memory Usage: 8.6 MB, less than 21.93% of C++ online submissions for Fibonacci Number.
29class Solution {
30public:
31    int fib(int N) {
32        if(N==0 or N==1){
33            return N;
34        }
35        vector<int> arr = {0, 1};
36        while(arr.size() < N+1){
37            arr.push_back(arr[arr.size()-1] + arr[arr.size()-2]);
38        }
39        return arr[arr.size()-1];
40    }
41};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.