Improving my skills in optimizing algorithms and understanding big-o notation is a priority for me.
I devised the following function to compute the n-th Fibonacci number. It works well for higher inputs, but I wonder how I can enhance it. What are the downsides of calculating the Fibonacci sequence in this manner?
function fibo(n) {
var i;
var resultsArray = [];
for (i = 0; i <= n; i++) {
if (i === 0) {
resultsArray.push(0);
} else if (i === 1) {
resultsArray.push(1);
} else {
resultsArray.push(resultsArray[i - 2] + resultsArray[i - 1]);
}
}
return resultsArray[n];
}
I suspect that my time complexity is O(n), while the space complexity could be O(n^2) due to the array creation. Is my analysis correct?