Questions tagged [space-complexity]

The space complexity of an algorithm quantifies the amount of memory taken by an algorithm to run as a function of the size of the input to the problem. The space complexity of an algorithm is commonly expressed using big O notation, which suppresses multiplicative constants and lower order terms.

The space complexity of a program (for a given input) is the number of elementary objects that this program needs to store during its execution. This number is computed with respect to the size n of the input data.
Formally, for an algorithm T and an input x, DSPACE(T, x) denotes the number of cells used during the (deterministic) computation T(x). We will note DSPACE(T) = O(f (n)) if DSPACE(T, x) = O(f (n)) with n = |x | (length of x).

923 questions
3
votes
1 answer

Time/space complexity of in-built python functions

What is the time/space complexity of split/strip/open (in-built python functions)? Does anyone know where i can look up on the time/space complexity of these functions?
3
votes
2 answers

Sum Values of Python Dictionary (Time/Space Complexity)

I am attempting to solve the following problem: Given the list of birth dates and death dates, find the year in which the most people were alive. Here is my code thus far: b = [1791, 1796, 1691, 1907, 1999, 2001, 1907] # birth dates d = [1800, 1803,…
3
votes
2 answers

Space complexity- various cases functions involving arrays

here i have written some different cases of functions that have array as input i need help to determine if reasoning behind my answers is correct (i have put question marks on the cases im having difficulty with) case 1: algo(arr[],n){ //does…
3
votes
1 answer

What is the difference between american sort and radix sort?

I came across this sorting algorithm called american sort. I read it being a variant of radix sort. Can someone elaborate me regarding this sorting algorithm and also the time and space complexity related to it.
3
votes
3 answers

Should one use StringBuilder in a recursive function?

I understand that the purpose of StringBuilder (usually) is to avoid creating objects over and over in Java when iterating over strings, especially in a loop. I'm wondering if it's worth it to use it in a recursive function that returns a string.…
Zaya
  • 316
  • 4
  • 14
3
votes
2 answers

Memory Efficient Way to Remove Kth Node from End of Linked List

This is a known problem with several known solutions, but my current struggle is to try and find the most efficient way to solve it, considering memory usage (and not time-complexity). The problem: Given a singly-linked-list of unknown (but…
Assafs
  • 3,257
  • 4
  • 26
  • 39
3
votes
1 answer

Time and Space Complexity of list to str conversion in Python

Trying to find out what is the time complexity of casting to string str([1,2,6,...,3,6]) Pretty sure it's O(1) Not sure how to verify. Edit: about space complexity, That should not be linear to list size, thinking O(1) because string has max…
WebQube
  • 8,510
  • 12
  • 51
  • 93
3
votes
1 answer

Is it possible to get the height of a binary tree without using recursion or a stack/queue?

I am writing a program in C that involves many calls to a function that returns the height of a binary tree. Initially I used recursion to do it, but that has soon come back to bite me because I am getting stack overflow errors (not due to infinite…
Robbie
  • 1,733
  • 2
  • 13
  • 20
3
votes
1 answer

Difference between Auxiliary Space and Space Complexity of Heap Sort?

Difference between Auxiliary Space and Space Complexity of Heap Sort? My attempt: As explained here: If we want to compare standard sorting algorithms on the basis of space, then Auxiliary Space would be a better criteria than Space Complexity.…
3
votes
2 answers

Merging two sorted linked list--understanding why it's O(1) vs. O(N) space complexity

The majority of implementations I've seen for merging two sorted linked lists iteratively are as follows. Create a dummy node. Point it to the linked list head that has the smaller value. Move that head to its next node. Move dummy pointer to its…
segue_segway
  • 1,490
  • 1
  • 22
  • 40
3
votes
3 answers

Why does the following code only have space complexity O(N)?

This is taken straight from Cracking the coding interview by Gayle Lakmaan McDowell. She lists the following code: int f(int n) { if(n<=1){ return 1; } return f(n-1) + f(n-1); } She has an articulate explanation about why the time…
Django
  • 343
  • 1
  • 4
  • 23
3
votes
3 answers

Algorithm to print all valid combations of n pairs of parenthesis

I'm working on the problem stated in the question statement. I know my solution is correct (ran the program) but I'm curious as to whether or not I'm analyzing my code (below) correctly. def parens(num) return ["()"] if num == 1 paren_arr = [] …
segue_segway
  • 1,490
  • 1
  • 22
  • 40
3
votes
3 answers

Why is the BigO of adjacency list for a graph is (V + E) and not (V^2)?

Correct me if my thinking is wrong. I think that BigO(V + E) = BigO(V^2). Below is my thinking: Edges in a complete graph = n*(n-1)/2. Switching from E and V to n because it is easier for me to think that way. E = n*(n-1)/2 V = n BigO(V + E)…
3
votes
2 answers

How to compute kolmogorov complexity of an algorithm?

Suppose for various input strings an algorithm generates binary string with same number of 0's and 1's. The output for two different input strings may or may not be the same. Can we say anything about the space complexity of the algorithm?
Debajyoti
  • 31
  • 2
3
votes
1 answer

Is there a general name for the type of complexity notated by Big O?

Big O describes the concepts of time complexity and space complexity, but is there a more general higher-level category which describes the domain of the complexity described by Big O notation? For example, if someone brought up the topic of…