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
2
votes
1 answer

2 Player Game is Polynomial Space complete

So there is a n x n game board and each location on the board has an integer. Player one picks a number from row 1 and player 2 picks a number from row 2 and they alternate until there are no more rows. Then they add up all the numbers and player 1…
2
votes
2 answers

Approaching space complexities for large projects

I got this question asked in a startup If you are asked to design Maps like Bing Maps how would you estimate the space complexity ? The only answer I could think for maps is, the space was constant, but I was not really sure if I went in the right…
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
2
votes
1 answer

How can Morris inorder tree traversal have a space complexity of O(1)?

I was reading about Morris inorder Traversal (geekforgeeks, other SO question). It seems that the advantage of Morris Traversal as opposed to recursion or iteration with a stack, is that the space complexity is O(1) instead of O(h) with h the height…
Ricola
  • 2,621
  • 12
  • 22
2
votes
0 answers

Is my assumption about Time and Space Complexity of subsum algorithm correct?

Is the space complexity of this algorithm is O(n^3) cubic space complexity ? As: list is an array, subs is an array + one array created with map. Is the time complexity is O(n^4) biquadratic? As: two times loop on list + one time map + one time…
Daria
  • 29
  • 1
2
votes
1 answer

Algorithm Time and Space Complexity

It is a Textbook Assignment question in my Sophomore Tried solving it for 3 days but having a hard time. The question is: Find the Time and Space complexity , along with the Value Returned and No of function calls rec(n) { if( n <= 2 ) …
2
votes
1 answer

Space complexity of "Number of islands" BFS solution on Leetcode

For the Leetcode question "Number of islands," I am trying to determine the SPACE complexity for the Breadth First Search solution. from collections import deque class Solution: def numIslands(self, grid: List[List[str]]) -> int: …
2
votes
2 answers

how to manipulate very long string to avoid out of memory with golang

I trying for personal skills improvement to solve the hacker rank challenge: There is a string, s, of lowercase English letters that is repeated infinitely many times. Given an integer, n, find and print the number of letter a's in the first n…
Ben
  • 63
  • 2
  • 9
2
votes
2 answers

Space Complexity in Breadth First Search (BFS) Algorithm

According to Artificial Intelligence A Modern Approach - Stuart J. Russell , Peter Norvig (Version 4), space complexity of BFS is O(b^d), where 'b' is branching factor and 'd' is depth. Complexity of BFS is obtained by this assumption: we store all…
2
votes
5 answers

Relation of time complexity and space complexity

Can an algorithm having a time complexity of O(n) have a space complexity of O(n2) or more than that?
noddy
  • 1,010
  • 4
  • 14
  • 27
2
votes
1 answer

What is the time complexity of this python function which includes slice operations?

I am learning python slice operations and I decided to write a simple function that iterates through a string with a window of size k and adds the window to the dictionary along with its frequency. So for example if the string input is "abab" and k…
2
votes
1 answer

How do I solve this recurrence equation T(n) = T(n-1)+n^2 using the unfolding method?

I try to solve this equations: T(n) = T(n-1) + n^2 for n>1 T(1) = 1 for n=1 initial value knowing that: Σ between i=0 and i=n-1 of ^2 = (+1)(2+1)/6 I proceed as follow: T(n-1) = T(n-2) + (n-1)^2 T(n-2) = T(n-3) + (n-2)^2 T(n-3) = T(n-4)…
ire
  • 23
  • 1
  • 7
2
votes
3 answers

What are the fundamentals of calculating space complexity in loops?

Imagine you loop n times, and every iteration you create a string of space n with scope only within that iteration (thus it is no longer accessible in the next iteration). I would look and say that I use O(n^2) space because for n iterations, I use…
2
votes
1 answer

How to analyse space complexity of algorithm for CountNonDivisible?

I'm trying to analyse the worst-case space complexity of this algorithm for solving Codility's CountNonDivisible problem. The problem statement: You’re given an array A consisting of N integers. For each number A[i] such that 0 ≤ i < N, we want to…
2
votes
1 answer

Generating a sequence of n random numbers without duplicates with a space complexity of O(log(n))

I would like to generate a sequence of n random integers in the interval [1,n] without duplicates, i.e. a permutation of the sequence [1,2,...,n] with O(log(n)) space complexity (or a polynomial function of log(n)). One hint is that I can assume…
user445308
  • 21
  • 2
2
votes
1 answer

How to reduce the complexity and automate my approach for building a struct?

I have the following macro and it should allow me to build a struct with one or 2 arguments only! macro baseStruct(name, arg) if length(arg.args)==2 && isa(arg.args[1],Symbol) || length(arg.args)==1 aakws = Pair{Symbol,Any}[] …
ahm5
  • 633
  • 5
  • 9