1. let a = [];
  2. a[1] = 1;
  3. console.log(a); // [empty, 1]
  1. function createEmptyMatrix(n) {
  2. const matrix = [];
  3. for (let i=0; i<n; i++) {
  4. matrix.push([]);
  5. }
  6. return matrix;
  7. }
  8. createEmptyMatrix(2) //[ [], [] ]

Runtime Complexity

Space Complexity

image.png

image.png
image.png


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);
}

  1. // terrible horrible runtime complexity (exponential)!!!
  2. function fib(n) {
  3. if (n < 2) return n;
  4. return fib(n-1) + fib(n-2);
  5. }