FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/DynamicProgramming/Fibonacci.java at master · GADNT/Java · GitHub
GADNT
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
DynamicProgramming
/
Fibonacci.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
91 lines (77 loc) · 2.25 KB
Breadcrumbs
Java
/
DynamicProgramming
/
Fibonacci.java
Copy path
File metadata and controls
91 lines (77 loc) · 2.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
DynamicProgramming
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Map
;
import
java
.
util
.
Scanner
;
/** @author Varun Upadhyay (https://github.com/varunu28) */
public
class
Fibonacci
{
private
static
Map
<
Integer
,
Integer
>
map
=
new
HashMap
<>();
public
static
void
main
(
String
[]
args
) {
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
Scanner
sc
=
new
Scanner
(
System
.
in
);
int
n
=
sc
.
nextInt
();
System
.
out
.
println
(
fibMemo
(
n
));
System
.
out
.
println
(
fibBotUp
(
n
));
System
.
out
.
println
(
fibOptimized
(
n
));
sc
.
close
();
}
/**
* This method finds the nth fibonacci number using memoization technique
*
* @param n The input n for which we have to determine the fibonacci number Outputs the nth
* fibonacci number
*/
public
static
int
fibMemo
(
int
n
) {
if
(
map
.
containsKey
(
n
)) {
return
map
.
get
(
n
);
}
int
f
;
if
(
n
<=
1
) {
f
=
n
;
}
else
{
f
=
fibMemo
(
n
-
1
) +
fibMemo
(
n
-
2
);
map
.
put
(
n
,
f
);
}
return
f
;
}
/**
* This method finds the nth fibonacci number using bottom up
*
* @param n The input n for which we have to determine the fibonacci number Outputs the nth
* fibonacci number
*/
public
static
int
fibBotUp
(
int
n
) {
Map
<
Integer
,
Integer
>
fib
=
new
HashMap
<>();
for
(
int
i
=
0
;
i
<=
n
;
i
++) {
int
f
;
if
(
i
<=
1
) {
f
=
i
;
}
else
{
f
=
fib
.
get
(
i
-
1
) +
fib
.
get
(
i
-
2
);
}
fib
.
put
(
i
,
f
);
}
return
fib
.
get
(
n
);
}
/**
* This method finds the nth fibonacci number using bottom up
*
* @param n The input n for which we have to determine the fibonacci number Outputs the nth
* fibonacci number
* <p>This is optimized version of Fibonacci Program. Without using Hashmap and recursion. It
* saves both memory and time. Space Complexity will be O(1) Time Complexity will be O(n)
* <p>Whereas , the above functions will take O(n) Space.
* @author Shoaib Rayeen (https://github.com/shoaibrayeen)
*/
public
static
int
fibOptimized
(
int
n
) {
if
(
n
==
0
) {
return
0
;
}
int
prev
=
0
,
res
=
1
,
next
;
for
(
int
i
=
2
;
i
<=
n
;
i
++) {
next
=
prev
+
res
;
prev
=
res
;
res
=
next
;
}
return
res
;
}
}
Back
|
FazBrowse Home
|
New Git URL