hello-algo/codes/javascript/chapter_computational_complexity/space_complexity.js
Justin Tse bc88e52955
Add JavaScript and TypeScript code and docs for Section Space Complexity (#331)
* Fix bug before commit 5eae708

* Update queue.md

* Update the coding style for JavaScript

* Add JavaScript and TypeScript code for Section Space Complexity

* Add JavaScript and TypeScript code to docs for Section Space Complexity

* Update hashing_search.js

---------

Co-authored-by: Yudong Jin <krahets@163.com>
2023-02-06 01:24:22 +08:00

101 lines
2.3 KiB
JavaScript

/**
* File: space_complexity.js
* Created Time: 2023-02-05
* Author: Justin (xiefahit@gmail.com)
*/
const { ListNode } = require('../include/ListNode');
const { TreeNode } = require('../include/TreeNode');
const { printTree } = require('../include/PrintUtil');
/* 函数 */
function constFunc() {
// do something
return 0;
}
/* 常数阶 */
function constant(n) {
// 常量、变量、对象占用 O(1) 空间
const a = 0;
const b = 0;
const nums = new Array(10000);
const node = new ListNode(0);
// 循环中的变量占用 O(1) 空间
for (let i = 0; i < n; i++) {
const c = 0;
}
// 循环中的函数占用 O(1) 空间
for (let i = 0; i < n; i++) {
constFunc();
}
}
/* 线性阶 */
function linear(n) {
// 长度为 n 的数组占用 O(n) 空间
const nums = new Array(n);
// 长度为 n 的列表占用 O(n) 空间
const nodes = [];
for (let i = 0; i < n; i++) {
nodes.push(new ListNode(i));
}
// 长度为 n 的哈希表占用 O(n) 空间
const map = new Map();
for (let i = 0; i < n; i++) {
map.set(i, i.toString());
}
}
/* 线性阶(递归实现) */
function linearRecur(n) {
console.log(`递归 n = ${n}`);
if (n === 1) return;
linearRecur(n - 1);
}
/* 平方阶 */
function quadratic(n) {
// 矩阵占用 O(n^2) 空间
const numMatrix = Array(n).fill(null).map(() => Array(n).fill(null));
// 二维列表占用 O(n^2) 空间
const numList = [];
for (let i = 0; i < n; i++) {
const tmp = [];
for (let j = 0; j < n; j++) {
tmp.push(0);
}
numList.push(tmp);
}
}
/* 平方阶(递归实现) */
function quadraticRecur(n) {
if (n <= 0) return 0;
const nums = new Array(n);
console.log(`递归 n = ${n} 中的 nums 长度 = ${nums.length}`);
return quadraticRecur(n - 1);
}
/* 指数阶(建立满二叉树) */
function buildTree(n) {
if (n === 0) return null;
const root = new TreeNode(0);
root.left = buildTree(n - 1);
root.right = buildTree(n - 1);
return root;
}
/* Driver Code */
const n = 5;
// 常数阶
constant(n);
// 线性阶
linear(n);
linearRecur(n);
// 平方阶
quadratic(n);
quadraticRecur(n);
// 指数阶
const root = buildTree(n);
printTree(root);