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

Python function time complexity

I am wondering if I calculate the time complexity correctly with the function below. mat is a list of lists. k is an integer. def kWeakestRows(mat, k): hashmap = {} for i in range(len(mat)): hashmap[i] = Counter(mat[i]).get(1) …
Dominic
  • 7
  • 2
-1
votes
1 answer

Is this implementation of a graph effectively linear in the number of edges?

I have a class which resembles an adjacency matrix representation of a weighted, directed graph. Let's suppose the graph has n vertices. Here is how it works: At first, allocate n2 slots to hold integers (stored in a variable named graph), in the…
Wais Kamal
  • 5,858
  • 2
  • 17
  • 36
-1
votes
1 answer

BFS and DFS complexity

I didn't understand these complexities of BFS and DFS for BFS it's written that time complexity is (d is the depth of the solution node in the tree and b is the maximum number of node sons) the space complexity is written to be for DFS time…
-1
votes
1 answer

What is the Space complexity of storing a string in JavaScript?

Are storing strings O(1) space? I am really confused about this since a mix of people saying its O(1) and O(n) at the same time.... Could someone help me on this one? I really need it for practicing for my interviews. Thank you!!
wiZzz
  • 67
  • 1
  • 4
-1
votes
1 answer

Why does the algorithm with space complexity O(n) use less memory than the one with O(1) complexity?

The question concerns LeetCode challenge #2: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return…
-1
votes
1 answer

What's the Time Complexity and Space Complexity this function to check valid parenthesis?

I am unable to find the Time Complexity and Space Complexity of the code to check valid parenthesis. Can anyone help, please? code - bool isValid(string s) { int s_size = s.length(); for (int i = 0; i < s_size-1;i++) { …
-1
votes
2 answers

Time and space complexity using Tail recursion

Can anyone please help me to understand the time and space complexity of algo to balance parenthesis def isValid(s: String): Boolean = { @annotation.tailrec def go(i: Int, stack: List[Char]): Boolean = { if (i >= s.length) { stack.isEmpty }…
coder25
  • 2,363
  • 12
  • 57
  • 104
-1
votes
1 answer

Why method 2 is faster then method 1 even when method 2 is using O(2n) time complexity and method 1 is solving same problem in O(n)

I was solving Majority Element problem of GFG. I had done same problem with 2 approaches Method 1 static int majorityElement1(int a[], int size) { HashMap mp = new HashMap<>(); int count = 0; int maxNum =…
-1
votes
1 answer

Time and space complexity of this function

def f3(n): if n <= 1: return 1 L = [i for i in range(n)] return 2 * f3(n // 2) + 1 I am trying to calculate the time and space complexity of this function, and I have a very important question about the last part 2*f3(n//2): do we…
Pwaol
  • 206
  • 1
  • 7
-1
votes
3 answers

Time and Space Complexity of python function

Can you please help me with how to calculate space and time complexity for below code def isPalindrome(string): # Write your code here. string1=string[::-1] if string1==string: return True else : return False
-1
votes
1 answer

Python sorted() function space comlexity

I know that sort() is O(1) space since the sorting is in place. However, sorted() function returns a new list with sorted elements. Since sorted() returns a new array with sorted elements, does this mean that an algorithm that uses the sorted()…
-1
votes
1 answer

How can I solve the space complexity of DFS in terms of maximum branching factor, depth of the optimal solution and maximum tree depth?

What is the space complexity of DFS in terms of maximum branching factor, depth of the optimal solution and maximum tree depth? Show the necessary calculation and write a logical explanation.
-1
votes
1 answer

Need some help/confirmation for some time and space complexity problems in Java

I want to make sure that my way of working with complexity of space and time is good. I am an IT student and I am working part time and I got this exercise besides my work for a course I am following by the company I'm working for and I wonder if I…
-1
votes
2 answers

Time Complexity and Space Complexity of the Python Code

Can someone please help me with the time and space complexity of this code snippet? Please refer to the leetcode question- Word break ii.Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to…
-1
votes
2 answers

Javascript: finding minimum sum after k operations

An array is manipulated k times so that each time the max value is divided by 2 and rounded up. I need to find its minimum sum after these k manipulations. k and all numbers in array num > 1. The method minSum receives an array called num and an…
Nimish goel
  • 2,561
  • 6
  • 27
  • 42