mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-03 19:03:42 +08:00
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
104 lines
2.4 KiB
JavaScript
104 lines
2.4 KiB
JavaScript
/**
|
|
* File: space_complexity.js
|
|
* Created Time: 2023-02-05
|
|
* Author: Justin (xiefahit@gmail.com)
|
|
*/
|
|
|
|
const { ListNode } = require('../modules/ListNode');
|
|
const { TreeNode } = require('../modules/TreeNode');
|
|
const { printTree } = require('../modules/PrintUtil');
|
|
|
|
/* Function */
|
|
function constFunc() {
|
|
// Perform some operations
|
|
return 0;
|
|
}
|
|
|
|
/* Constant order */
|
|
function constant(n) {
|
|
// Constants, variables, objects occupy O(1) space
|
|
const a = 0;
|
|
const b = 0;
|
|
const nums = new Array(10000);
|
|
const node = new ListNode(0);
|
|
// Variables in the loop occupy O(1) space
|
|
for (let i = 0; i < n; i++) {
|
|
const c = 0;
|
|
}
|
|
// Functions in the loop occupy O(1) space
|
|
for (let i = 0; i < n; i++) {
|
|
constFunc();
|
|
}
|
|
}
|
|
|
|
/* Linear order */
|
|
function linear(n) {
|
|
// Array of length n uses O(n) space
|
|
const nums = new Array(n);
|
|
// A list of length n occupies O(n) space
|
|
const nodes = [];
|
|
for (let i = 0; i < n; i++) {
|
|
nodes.push(new ListNode(i));
|
|
}
|
|
// A hash table of length n occupies O(n) space
|
|
const map = new Map();
|
|
for (let i = 0; i < n; i++) {
|
|
map.set(i, i.toString());
|
|
}
|
|
}
|
|
|
|
/* Linear order (recursive implementation) */
|
|
function linearRecur(n) {
|
|
console.log(`Recursion n = ${n}`);
|
|
if (n === 1) return;
|
|
linearRecur(n - 1);
|
|
}
|
|
|
|
/* Exponential order */
|
|
function quadratic(n) {
|
|
// Matrix uses O(n^2) space
|
|
const numMatrix = Array(n)
|
|
.fill(null)
|
|
.map(() => Array(n).fill(null));
|
|
// 2D list uses O(n^2) space
|
|
const numList = [];
|
|
for (let i = 0; i < n; i++) {
|
|
const tmp = [];
|
|
for (let j = 0; j < n; j++) {
|
|
tmp.push(0);
|
|
}
|
|
numList.push(tmp);
|
|
}
|
|
}
|
|
|
|
/* Quadratic order (recursive implementation) */
|
|
function quadraticRecur(n) {
|
|
if (n <= 0) return 0;
|
|
const nums = new Array(n);
|
|
console.log(`In recursion n = ${n}, nums length = ${nums.length}`);
|
|
return quadraticRecur(n - 1);
|
|
}
|
|
|
|
/* Driver Code */
|
|
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 order
|
|
constant(n);
|
|
// Linear order
|
|
linear(n);
|
|
linearRecur(n);
|
|
// Exponential order
|
|
quadratic(n);
|
|
quadraticRecur(n);
|
|
// Exponential order
|
|
const root = buildTree(n);
|
|
printTree(root);
|