mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-06-05 15:09:09 +09:00
Update fibonacci-solution.js
Better O(n) time
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
const fibonacci = function(count) {
|
||||
if (count < 0) return "OOPS"
|
||||
const fibPart = [0, 1];
|
||||
for (let index = 1; index < count; index++) {
|
||||
fibPart.push(fibPart[index] + fibPart[index -1]);
|
||||
}
|
||||
return fibPart[count];
|
||||
if (count < 0) return "OOPS";
|
||||
if (count === 0) return 0;
|
||||
|
||||
let first_prev = 1;
|
||||
let second_prev = 0;
|
||||
|
||||
for (let i = 2; i <= count; i++) {
|
||||
let curr = first_prev + second_prev;
|
||||
second_prev = first_prev;
|
||||
first_prev = curr;
|
||||
}
|
||||
|
||||
return first_prev;
|
||||
};
|
||||
|
||||
module.exports = fibonacci;
|
||||
|
||||
Reference in New Issue
Block a user