> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/algo/dynamic-programming/fibonacci-number.md).

# Fibonacci Number

{% embed url="<https://leetcode.com/problems/fibonacci-number>" %}

## Solution 1

```java
class Solution {
    public int fib(int n) {
        if (n == 0) return 0;
        if (n == 1) return 1;
        
        int[] dp = new int[n + 1];
        dp[0] = 0;
        dp[1] = 1;
        for (int i = 2; i < n + 1; i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        
        return dp[n];
    }
}
```

This the bottom-up tabulation approach. We start from the very "bottom" and add up the numbers until `n`.

Time complexity: `O(n)`

Space complexity: `O(n)`

## Solution 2

```java
class Solution {
    private Map<Integer, Integer> map = new HashMap<>();
    
    public int fib(int n) {
        if (n <= 1) return n;
        map.putIfAbsent(0, 0);
        map.putIfAbsent(1, 1);
        if (map.containsKey(n)) {
            return map.get(n);
        }
        map.put(n, fib(n - 1) + fib(n - 2));
        return map.get(n);
}
```

This is the top-down memoization approach. We use a hash table to achieve memoization.

Time complexity: `O(n)`

Space complexity: `O(n)`

## Solution 3

```java
class Solution {
    public int fib(int n) {
        if (n <= 1) return n;
        int x = 0, y = 1, z = 1;
        
        for (int i = 3; i < n + 1; i++) {
            x = y;
            y = z;
            z = x + y;
        }
        return z;
    }
}
// x y z
//   x y z
// 0 1 1 2 3 5 8
```

This is the bottom-up approach without using an array to keep the results. Instead, we only use three variables to keep the progress.

Time complexity: `O(n)`

Space complexity: `O(1)`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://blog.yushunchen.com/algo/dynamic-programming/fibonacci-number.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
