-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFibonacciNumber.cs
93 lines (87 loc) · 2.84 KB
/
FibonacciNumber.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Text;
namespace LeetCodeSolutionsLib
{
/// <summary>
/// 509. Fibonacci Number
/// The Fibonacci numbers, commonly denoted to F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of two preceeding ones, start from 0 and 1.
/// IE) Input : [4]
/// Output : [3]
/// Explanation: F(4) = F(3) + F(2) ==> 2 + 1 = 3
/// </summary>
public class FibonacciNumber : Solution
{
private int n;
public FibonacciNumber(int n)
{
this.n = n;
}
private int fib(int n)
{
/*
// Base cases
int result = 0;
if (n == 0) return result;
if (n == 1 || n == 2)
{
result = 1;
}
// Otherwise use recursion to calculate the Fibonacci Number
else
{
result = fib(n - 1) + fib(n - 2);
}
return result;
*/
int?[] memo = new int?[n+1];
return fibMemoization(n, memo);
}
// Using Memoization to cache Fibonacci results
// Improves Time Complexity from O(2^n) to O(n)
private int fibMemoization(int n, int?[] memo)
{
// If the result has already been calculated, it will exist in the memo array.
if (memo[n] != null) return (int) memo[n];
int result = 0;
if (n == 0) return result;
if (n == 1 || n == 2)
{
result = 1;
}
else
{
result = fib(n - 1) + fib(n - 2);
memo[n] = result;
}
return result;
}
private int fibBottomUp(int n)
{
// Time Complexity : O(n)
// Space Complexity: O(n), where n is the input number
// Base Cases
if (n == 0) return 0;
if (n == 1 || n == 2) return 1;
// Build the Fibonacci Sequence from left to right iteratively
int[] bottomUp = new int[n + 1];
bottomUp[1] = 1;
bottomUp[2] = 1;
for (int i = 3; i < bottomUp.Length; i++)
{
bottomUp[i] = bottomUp[i - 1] + bottomUp[i - 2];
}
return bottomUp[n];
}
public override void PrintExample()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
var results = fibBottomUp(this.n);
watch.Stop();
Console.WriteLine($"509. Fibonacci Number\n" +
$"Input Num = {this.n} \n" +
$"Result: [{results}] \n" +
$"Execution Speed: {watch.ElapsedMilliseconds}ms \n");
}
}
}