Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions src/FibonacciNumber509.java

This file was deleted.

60 changes: 60 additions & 0 deletions src/main/java/com/leetcode/easy/FibonacciNumber509.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Tags: Memoization, Dynamic Programming
package com.leetcode.easy;

import java.util.HashMap;
import java.util.Map;

public class FibonacciNumber509 {
private int fibRecursive(int n, Map<Integer, Integer> memo) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
if (memo.containsKey(n)) {
return memo.get(n);
}
memo.put(n, this.fibRecursive(n - 1, memo) + this.fibRecursive(n - 2, memo));
return memo.get(n);
}

/*
Time complexity: O(N).
Each number, starting at 2 up to and including N, is visited, computed and then stored for O(1) access later on.

Space complexity: O(N).
The size of the stack in memory is proportional to N.
Also, the memoization hash table is used, which occupies O(N) space.
*/
public int fibMemoization(int n) {
Map<Integer, Integer> memo = new HashMap<>();
return this.fibRecursive(n, memo);
}

/*
Time complexity: O(N).
Each value from 2 to N is computed once.
Thus, the time it takes to find the answer is directly
proportional to N where N is the Fibonacci Number we are looking to compute.

Space complexity: O(1).
This requires 1 unit of space for the integer N and 3 units of space to store the computed values
(curr, prev1, and prev2) for every loop iteration.
The amount of space used is independent of N, so this approach uses a constant amount of space.
*/
public int fibDp(int n) {
if (n <= 1) {
return n;
}
int curr = 0;
int prev1 = 1;
int prev2 = 0;
for (int i = 2; i <= n; i++) {
curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
}
return curr;
}
}
75 changes: 75 additions & 0 deletions src/test/java/com/leetcode/easy/FibonacciNumber509Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.leetcode.easy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class FibonacciNumber509Test {
private FibonacciNumber509 solution;

@BeforeEach
void setUp() {
solution = new FibonacciNumber509();
}

@Test
void testFibMemoization_Example1() {
int n = 2;
int expected = 1;

int result = solution.fibMemoization(n);

assertEquals(expected, result);
}

@Test
void testFibMemoization_Example2() {
int n = 3;
int expected = 2;

int result = solution.fibMemoization(n);

assertEquals(expected, result);
}

@Test
void testFibMemoization_Example3() {
int n = 4;
int expected = 3;

int result = solution.fibMemoization(n);

assertEquals(expected, result);
}

@Test
void testDp_Example1() {
int n = 2;
int expected = 1;

int result = solution.fibDp(n);

assertEquals(expected, result);
}

@Test
void testDp_Example2() {
int n = 3;
int expected = 2;

int result = solution.fibDp(n);

assertEquals(expected, result);
}

@Test
void testDp_Example3() {
int n = 4;
int expected = 3;

int result = solution.fibDp(n);

assertEquals(expected, result);
}
}