-
Notifications
You must be signed in to change notification settings - Fork 16
/
Fibonacci.swift
46 lines (36 loc) · 1.21 KB
/
Fibonacci.swift
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
// Author: Sayed Mahmudul Alam
// Source: LeetCode-509: https://www.geeksforgeeks.org/introduction-to-recursion-data-structure-and-algorithm-tutorials/
// Solution Ref: https://www.section.io/engineering-education/introduction-to-dynamic-programming/#:~:text=Dynamic%20Programming%20(DP)%20is%20an,optimal%20solution%20to%20its%20subproblems.
// Example Input: n = 3
// Output: 2
// Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
class RecursiveSolution {
func fib(_ n: Int) -> Int {
if n == 0 {
return 0
}
if n == 1 || n == 2 {
return 1
}
return fib(n-1) + fib(n-2)
}
}
class DPSolution {
func fib(_ n: Int) -> Int {
guard n > 1 else {
return n
}
// The first two values of the series are 0 and 1
var first = 0
var second = 1
// Since first two values are known, the loops can be started from 2
for i in 2...n {
// current term is the sum of previous two values
let term = first + second
// Now first term is second term and second term is the current term
first = second
second = term
}
return second
}
}