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.
space-complexity
941
questions
-4
votes
1
answer
70
views
does spacing between statements, indentation and comments in code affects its time and space complexity? [closed]
I'm trying to understand how code formatting and documentation impacts the performance of a program
Spacing between statements: having more or fewer blank lines between code statements.
Indentation: ...
2
votes
2
answers
81
views
I want to know the space complexity of this function
I was writing this code, and I am confused about whether the space complexity of this question is O(N) or O(N^2).
This is the function whose complexity i want to know:
static pair[] allPairs(int x, ...
2
votes
1
answer
77
views
Why can't a time complexity O(2^N ) be reduced to O(2*2*2*...*2^0)=O(1)?
Consider the time complexity O(5N2). By eliminating the constant factor 5, we represent the time complexity as O(N2).
Now, if we have a time complexity of O(2N), we can rewrite this as O(2⋅2N-1). Then ...
1
vote
0
answers
42
views
Time and space complexity of a given Branch-and-Bound algorithm
I need to calculate the time and space complexity of a Branch-and-Bound algorithm, to solve a problem. The problem is: On a chess board, the knight needes to move, from a initial to a end point, doing ...
-2
votes
0
answers
61
views
Space Complexity of subsets code when using bitmask
Consider the statement :
string bitmask = bitset<32>(i).to_string().substr(32 - n);
n is the size of a vector.
What should be the space complexity for this ?
My take is that it should be O(logN)....
0
votes
0
answers
20
views
Ranking 6 algorithms based on complexity
Problem: The file numere.in stores at least 3 numbers and at most 1000000 natural numbers with up to nine digits. It asks you to display on the screen how many digits were used to write these numbers.
...
0
votes
0
answers
49
views
Does output count for space complexity in an algorithm?
It's controversial when I search answers on the internet. Most people tend to omit the space of output. However, when I search the space complexity of Floyd-Warshall algorithm, all shown are O(n^2) ...
0
votes
1
answer
67
views
What is the space complexity of the following code?
What is the space complexity of the following code?
def f(n):
sum = 0
if n == 0:
return 1
else:
for i in range(n):
sum = sum + f(i)
return sum
So, this is ...
0
votes
1
answer
87
views
Duplicate Subtree in a Binary tree Time and space complexity
I saw the following question on GFG to find if there's a duplicate subtree of size 2 or more in a binary tree.
Now, it is written in the practice question requirements and also articles everywhere say ...
0
votes
2
answers
44
views
Space complexity when looping through string slice in Python
Normally when creating a string slice in Python, it takes O(n) space:
s = "hello world"
s[1:] # O(n)
When you loop through a string slice, it makes sense that it also takes O(n) space:
s = &...
0
votes
0
answers
20
views
How Fast Does MongoDB Indices Grow in Size?
In a MongoDB database that is indexed at one field, called field I, if the logical size of the database is growing at the rate of n, what would the growth rate of the size of the indices be? (Let's ...
-1
votes
1
answer
147
views
Time Complexity of "in" keyword in python when using a list? Leetcode
I read a little bit everywhere that the "in" operator have a time complexity of O(n), yet I used it in a code and idk why but it's acting as if it have the time complexity O(1)
So I was ...
4
votes
1
answer
311
views
lru_cache vs dynamic programming, stackoverflow with one but not with the other?
I'm doing this basic dp (Dynamic Programming) problem on trees (https://cses.fi/problemset/task/1674/). Given the structure of a company (hierarchy is a tree), the task is to calculate for each ...
0
votes
1
answer
68
views
Space Complexity of a Recursion Function Involving Additional Self-Contained Auxiliary Space
I apologize for the poor wording of the question, but it seems that there is no other way to phrase it. Suppose we have the following code, which is just toy code to highlight my concern:
def dfs(root,...
0
votes
0
answers
17
views
Reducing Memory on Binary Search Tree Insertion
So I have just written a solution to binary tree insertion. It seems to be effecient but takes up so much space in comparison to others. I am wondering how I can get better at reducing space ...