Skip to content

Commit 49c982a

Browse files
committed
add: space complexity
1 parent 570353f commit 49c982a

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Constant Space - O(1)
3+
* @description
4+
* - Uses a fixed amount of memory
5+
* @param {number} a
6+
* @param {number} b
7+
* @returns
8+
*/
9+
function addNumbers(a, b) {
10+
return a + b;
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Linear Space - O(n)
3+
* @description
4+
* - Uses memoy proportional to input size.
5+
* @param {number} n
6+
*/
7+
function storeNumbers(n) {
8+
/** @type { number[] } */
9+
const arr = [];
10+
11+
for (let i = 0; i < n; i++) {
12+
arr.push(i);
13+
}
14+
15+
return arr;
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Quadratic Space - O(n²)
3+
* @description
4+
* - Nested structures increase memory usage.
5+
* @param {number} n
6+
*/
7+
function matrix(n) {
8+
let arr = new Array(n).fill().map(() => new Array(n).fill(0));
9+
return arr;
10+
}
11+
console.log(matrix(3));

0 commit comments

Comments
 (0)