let a = [];
a[1] = 1;
console.log(a); // [empty, 1]
function createEmptyMatrix(n) {
const matrix = [];
for (let i=0; i<n; i++) {
matrix.push([]);
}
return matrix;
}
createEmptyMatrix(2) //[ [], [] ]
Runtime Complexity
Space Complexity
Fabonacci
function fib(n, arr = [0, 1]) {
if (arr.length >= n+1) return arr[n];
arr[arr.length] = arr[arr.length-1] + arr[arr.length-2];
return fib(n, arr);
}
// terrible horrible runtime complexity (exponential)!!!
function fib(n) {
if (n < 2) return n;
return fib(n-1) + fib(n-2);
}