space-complexity

Find the k non-repeating elements in a list with “little” additional space

懵懂的女人 提交于 2019-11-30 10:39:28
问题 The original problem statement is this one: Given an array of 32bit unsigned integers in which every number appears exactly twice except three of them (which appear exactly once), find those three numbers in O(n) time using O(1) extra space. The input array is read-only. What if there are k exceptions instead of 3? It's easy to solve this in Ο(1) time and Ο(1) space if you accept a very high constant factor because of the input restriction (the array can have at most 2 33 entries): for i in

What is the space complexity of a recursive fibonacci algorithm?

♀尐吖头ヾ 提交于 2019-11-30 07:54:57
问题 This is the recursive implementation of the Fibonacci sequence from Cracking the Coding Interview (5th Edition) int fibonacci(int i) { if(i == 0) return 0; if(i == 1) return 1; return fibonacci(i-1) + fibonaci(i-2); } After watching the video on the time complexity of this algorithm, Fibonacci Time Complexity, I now understand why this algorithm runs in O(2 n ). However I am struggling with analyzing the space complexity. I looked online and had a question on this. In this Quora thread, the

Hamming numbers for O(N) speed and O(1) memory

喜欢而已 提交于 2019-11-30 05:44:42
问题 Disclaimer: there are many questions about it, but I didn't find any with requirement of constant memory. Hamming numbers is a numbers 2^i*3^j*5^k , where i, j, k are natural numbers. Is there a possibility to generate Nth Hamming number with O(N) time and O(1) (constant) memory? Under generate I mean exactly the generator, i.e. you can only output the result and not read the previously generated numbers (in that case memory will be not constant). But you can save some constant number of them

What is O(1) space complexity?

邮差的信 提交于 2019-11-30 05:00:43
I am having a hard time understanding what is O(1) space complexity. I understand that it means that the space required by the algorithm does not grow with the input or the size of the data on which we are using the algorithm. But what does it exactly mean? If we use an algorithm on a linked list say 1->2->3->4, to traverse the list to reach "3" we declare a temporary pointer. And traverse the list until we reach 3. Does this mean we still have O(1) extra space? Or does it mean something completely different. I am sorry if this does not make sense at all. I am a bit confused. To answer your

How do you find the space complexity of recursive functions such as this one?

Deadly 提交于 2019-11-30 02:36:20
问题 f (int n){ if (n<=0){ return 1; } return f(n-1) + f(n-1); } Suppose we did f(4). My thought was that it would be O(2^n), since then in order to find f(n-1) + f(n-1) we would have to push f(n-1) = f(3) to the call stack twice, and then f(2) four times the call stack, etc. However, the book I got this from says that is is O(n). Why is that true? 回答1: Let's imagine evaluating this for f(4) (the example you consider). Here's how it would go. The stack starts by looking like I need to compute f(4)

Is my analysis of space complexity correct?

▼魔方 西西 提交于 2019-11-29 07:13:34
This is problem 9.5 from Cracking the Coding Interview 5 th edition The Problem: Write a method to compute all permutations of a string Here is my solution, coded in Java(test it, it works :) ) public static void generatePerm(String s) { Queue<Character> poss = new LinkedList<Character>(); int len = s.length(); for(int count=0;count<len;count++) poss.add(s.charAt(count)); generateRecurse(poss, len, ""); } private static void generateRecurse(Queue<Character> possibles, int n, String word) { if(n==0) System.out.println(word); else { for(int count=0;count<n;count++) { char first = possibles

Why does QuickSort use O(log(n)) extra space?

左心房为你撑大大i 提交于 2019-11-28 20:11:36
I have implemented the below quicksort algorithm. Online I've read that it has a space requirement of O(log(n)). Why is this the case? I'm not creating any extra data structures. Is it because my recursion will use some extra space on the stack? If this is the case, is it possible to do it with less memory by not having it be recursive (instead making it iterative)? private static void quickSort (int[] array, int left, int right) { int index = partition(array, left, right); //Sort left half if (left < index - 1) quickSort(array, left, index - 1); //Sort right half if (index < right) quickSort

Space complexity of recursive function

人盡茶涼 提交于 2019-11-28 17:17:13
Given the function below: int f(int n) { if (n <= 1) { return 1; } return f(n - 1) + f(n - 1); } I know that the Big O time complexity is O(2^N) , because each call calls the function twice. What I don't understand is why the space/memory complexity is O(N) ? A useful way to approach these types of problems is by thinking of the recursion tree . The two features of a recursive function to identify are: The tree depth (how many total return statements will be executed until the base case) The tree breadth (how many total recursive function calls will be made) Our recurrence relation for this

How do i reduce the space complexity in Sieve of Eratosthenes to generate prime between a and b?

こ雲淡風輕ζ 提交于 2019-11-28 11:47:54
After getting through some of the SO posts , i found Sieve of Eratosthenes is the best & fastest way of generating prime numbers. I want to generate the prime numbers between two numbers, say a and b . AFAIK, in Sieve's method, the space complexity is O( b ). PS: I wrote Big-O and not Theta, because i don't know whether the space requirement can be reduced. Can we reduce the space complexity in Sieve of Eratosthenes ? If you have enough space to store all the primes up to sqrt(b) then you can sieve for the primes in the range a to b using additional space O(b-a). In Python this might look like

Is my analysis of space complexity correct?

核能气质少年 提交于 2019-11-28 00:49:59
问题 This is problem 9.5 from Cracking the Coding Interview 5 th edition The Problem: Write a method to compute all permutations of a string Here is my solution, coded in Java(test it, it works :) ) public static void generatePerm(String s) { Queue<Character> poss = new LinkedList<Character>(); int len = s.length(); for(int count=0;count<len;count++) poss.add(s.charAt(count)); generateRecurse(poss, len, ""); } private static void generateRecurse(Queue<Character> possibles, int n, String word) { if