mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-05 20:04:12 +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
111 lines
2.9 KiB
Java
111 lines
2.9 KiB
Java
/**
|
|
* File: space_complexity.java
|
|
* Created Time: 2022-11-25
|
|
* Author: krahets (krahets@163.com)
|
|
*/
|
|
|
|
package chapter_computational_complexity;
|
|
|
|
import utils.*;
|
|
import java.util.*;
|
|
|
|
public class space_complexity {
|
|
/* Function */
|
|
static int function() {
|
|
// Perform some operations
|
|
return 0;
|
|
}
|
|
|
|
/* Constant order */
|
|
static void constant(int n) {
|
|
// Constants, variables, objects occupy O(1) space
|
|
final int a = 0;
|
|
int b = 0;
|
|
int[] nums = new int[10000];
|
|
ListNode node = new ListNode(0);
|
|
// Variables in the loop occupy O(1) space
|
|
for (int i = 0; i < n; i++) {
|
|
int c = 0;
|
|
}
|
|
// Functions in the loop occupy O(1) space
|
|
for (int i = 0; i < n; i++) {
|
|
function();
|
|
}
|
|
}
|
|
|
|
/* Linear order */
|
|
static void linear(int n) {
|
|
// Array of length n uses O(n) space
|
|
int[] nums = new int[n];
|
|
// A list of length n occupies O(n) space
|
|
List<ListNode> nodes = new ArrayList<>();
|
|
for (int i = 0; i < n; i++) {
|
|
nodes.add(new ListNode(i));
|
|
}
|
|
// A hash table of length n occupies O(n) space
|
|
Map<Integer, String> map = new HashMap<>();
|
|
for (int i = 0; i < n; i++) {
|
|
map.put(i, String.valueOf(i));
|
|
}
|
|
}
|
|
|
|
/* Linear order (recursive implementation) */
|
|
static void linearRecur(int n) {
|
|
System.out.println("Recursion n = " + n);
|
|
if (n == 1)
|
|
return;
|
|
linearRecur(n - 1);
|
|
}
|
|
|
|
/* Exponential order */
|
|
static void quadratic(int n) {
|
|
// Matrix uses O(n^2) space
|
|
int[][] numMatrix = new int[n][n];
|
|
// 2D list uses O(n^2) space
|
|
List<List<Integer>> numList = new ArrayList<>();
|
|
for (int i = 0; i < n; i++) {
|
|
List<Integer> tmp = new ArrayList<>();
|
|
for (int j = 0; j < n; j++) {
|
|
tmp.add(0);
|
|
}
|
|
numList.add(tmp);
|
|
}
|
|
}
|
|
|
|
/* Quadratic order (recursive implementation) */
|
|
static int quadraticRecur(int n) {
|
|
if (n <= 0)
|
|
return 0;
|
|
// Array nums has length n, n-1, ..., 2, 1
|
|
int[] nums = new int[n];
|
|
System.out.println("In recursion n = " + n + ", nums length = " + nums.length);
|
|
return quadraticRecur(n - 1);
|
|
}
|
|
|
|
/* Driver Code */
|
|
static TreeNode buildTree(int n) {
|
|
if (n == 0)
|
|
return null;
|
|
TreeNode root = new TreeNode(0);
|
|
root.left = buildTree(n - 1);
|
|
root.right = buildTree(n - 1);
|
|
return root;
|
|
}
|
|
|
|
/* Driver Code */
|
|
public static void main(String[] args) {
|
|
int n = 5;
|
|
// Constant order
|
|
constant(n);
|
|
// Linear order
|
|
linear(n);
|
|
linearRecur(n);
|
|
// Exponential order
|
|
quadratic(n);
|
|
quadraticRecur(n);
|
|
// Exponential order
|
|
TreeNode root = buildTree(n);
|
|
PrintUtil.printTree(root);
|
|
}
|
|
}
|