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
12
votes
2 answers

Big O complexities of algorithms - LZW and Huffman

What are the space and time complexities, in Big O notation, for the Lempel-Ziv-Welch and Huffman compression algorithms? Google is failing me. Thanks, Francisco
F. P.
  • 5,018
  • 10
  • 55
  • 80
12
votes
2 answers

What is the worst case scenario for an unordered_map?

I have found many posts about the complexity of map and unordered_map. It is said that unordered_map has worst case complexity of O(N). For my purpose I will have input as sorted values like 1 2 5 6 9 11 12... I need to insert or find and delete a…
Tahlil
  • 2,680
  • 6
  • 43
  • 84
11
votes
1 answer

C++ Time and Space Complexity of string class erase member function

I wanted to know if someone knew the implementation of the C++ string::erase function and it's complexity. I know the C++ string is an object of characters. I'm assuming it doesn't allocate and create a new object of characters and then copy over…
B Stoops
  • 111
  • 1
  • 3
11
votes
3 answers

Is it possible to implement quicksort with O(1) space complexity?

From what I understood in Wikipedia's explanation of quicksort's space complexity, quicksort's space complexity comes from its recursive nature. I'm curious as to whether it's possible to implement quicksort non-recursively and, in doing so,…
Daniel
  • 2,944
  • 3
  • 22
  • 40
10
votes
4 answers

Python list.clear() time and space complexity?

I am writing a blogpost on Python list.clear() method where I also want to mention about the time and space complexity of the underlying algorithm. I expected the time complexity to be O(N), iterate over the elements and free the memory? But, I…
Pankaj Mishra
  • 550
  • 6
  • 18
10
votes
3 answers

Do variables declared in loop make space complexity O(N)?

Would variables declared inside of a for loop that loops N times make the space complexity O(N) even though those variables fall out of scope each time the loop repeats? for(var i = 0; i < N; i++){ var num = i + 5; }
spencer.sm
  • 19,173
  • 10
  • 77
  • 88
10
votes
5 answers

Space Complexity of an array?

I have an array of size N, and N is <=200. What would be the space complexity here. O(1) or (N) - considering the constraint N.
Ritt
  • 3,181
  • 3
  • 22
  • 51
10
votes
1 answer

HashMap Space Complexity

Here's a sample solution for "Populating Next Right Pointers in Each Node" puzzle: Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. public void connect(Node root)…
Yar
  • 7,020
  • 11
  • 49
  • 69
10
votes
3 answers

Space complexity of head.reverse vs. last

In many systems, head.reverse requires space proportional to the size of the list, whereas last requires constant space. Are there systems to perform such a transformation? Similarly for reverse.take n.reverse? Edit: I would like to extend my…
false
  • 10,264
  • 13
  • 101
  • 209
10
votes
2 answers

Space complexity of recursive algorithm

I was asked at an interview, the efficient way to solve a problem checking for pallindrome. Now i can do two things: starting from i = 0 to i = n/2 and comparing ith and n-ith character to be equal. I can use recursion to check if first and last…
dharam
  • 7,882
  • 15
  • 65
  • 93
9
votes
13 answers

Can we compute this in less than O(n*n) ...( nlogn or n)

This is a question asked to me by a very very famous MNC. The question is as follows ... Input an 2D N*N array of 0's and 1's. If A(i,j) = 1, then all the values corresponding to the ith row and the jth column are going to be 1. If there is a 1…
Flash
  • 2,901
  • 5
  • 38
  • 55
9
votes
3 answers

Is this function (for loop) space complexity O(1) or O(n)?

public void check_10() { for (string i : list) { Integer a = hashtable.get(i); if (a > 10) { hashtable.remove(i); } } } Would this be O(1) or O(n)? I'm guessing O(n), but isn't it reusing the spot of…
9
votes
4 answers

Meaning of the terms O(1) space and without using extra space

This is slightly confusing to me. What should be my approach of solving a given problem when the constraint is as follows: 1) Without using extra space: For e.g.: If I want to sort a given array, I have few ways of doing it. Bubble sort, which…
dharam
  • 7,882
  • 15
  • 65
  • 93
8
votes
1 answer

Tutorial on space complexity of algorithms

Possible Duplicate: Plain English explanation of Big O I have always struggled to calculate the Big-O time and space complexity of the algorithms I write. Can anybody please point to a good resource for studying more about space complexity of…
stressed_geek
  • 2,118
  • 8
  • 33
  • 45
8
votes
3 answers

What is O(1) space complexity in term of Javascript code with example

For an example I have below mentioned input and output for function reverseWords() It's a simple example but this will help me understand. How I will write a function which is In O(1) space for below request ? // var input = ['H', 'e', 'l', 'l',…
Javascript Coder
  • 5,691
  • 8
  • 52
  • 98
1 2
3
61 62