diff --git a/src/FibonacciNumber509.java b/src/FibonacciNumber509.java deleted file mode 100644 index bcc23ec..0000000 --- a/src/FibonacciNumber509.java +++ /dev/null @@ -1,30 +0,0 @@ -// Math, Dynamic Programming, Recursion, Memoization - -import java.util.Map; -import java.util.HashMap; - -public class FibonacciNumber509 { - private int dfs(int n, Map memo) { - if (n == 0) { - return 0; - } - if (n == 1) { - return 1; - } - if (memo.containsKey(n)) { - return memo.get(n); - } - memo.put(n, this.dfs(n - 1, memo) + this.dfs(n - 2, memo)); - return memo.get(n); - } - - public int fibMemoization(int n) { - Map memo = new HashMap<>(); - return this.dfs(n, memo); - } - - public static void main(String[] args) { - int n = 4; - System.out.println(new FibonacciNumber509().fibMemoization(n)); // 3 - } -} diff --git a/src/main/java/com/leetcode/easy/FibonacciNumber509.java b/src/main/java/com/leetcode/easy/FibonacciNumber509.java new file mode 100644 index 0000000..f8d9443 --- /dev/null +++ b/src/main/java/com/leetcode/easy/FibonacciNumber509.java @@ -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 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 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; + } +} diff --git a/src/test/java/com/leetcode/easy/FibonacciNumber509Test.java b/src/test/java/com/leetcode/easy/FibonacciNumber509Test.java new file mode 100644 index 0000000..8442d62 --- /dev/null +++ b/src/test/java/com/leetcode/easy/FibonacciNumber509Test.java @@ -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); + } +}