space-complexity

Sorting in linear time and in place

寵の児 提交于 2019-12-18 11:28:39
问题 Suppose that n records have keys in the range from 1 to k. Write an algorithm to sort the records in place in O(n+k) time. You may use O(k) storage outside the input array. Is your algorithm stable? if we use counting sort to we can do it in O(n+k) time and is stable but its not in place. if k=2 it can be done in place but its not stable (using two variables to maintain the indexes in the array for k=0 and k=1) but for k>2 i couldnt think of any good algo 回答1: First, let's rehash how counting

What is O(1) space complexity?

僤鯓⒐⒋嵵緔 提交于 2019-12-18 11:21:12
问题 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

How to determine memory and time complexity of an algorithm?

爷,独闯天下 提交于 2019-12-18 10:24:44
问题 I am not good at determining time and memory complexities and would appreciate it if someone could help me out. I have an algorithm, here and I am not sure what its time and memory complexities would be. Function sample(k) IF k < 2 Return 0 Return 1 + sample(k/2) What is its time and memory complexity and why? Thanks 回答1: Determining time and memory complexities amounts to counting how much of these two resources are used when running the algorithm, and seeing how these amounts change as the

Space complexity of recursive function

那年仲夏 提交于 2019-12-17 21:50:54
问题 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) ? 回答1: 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)

Regarding in-place merge in an array

陌路散爱 提交于 2019-12-17 09:31:19
问题 I came across the following question. Given an array of n elements and an integer k where k < n . Elements { a 0 ... a k } and { a k +1 ... a n } are already sorted. Give an algorithm to sort in O( n ) time and O(1) space. It does not seem to me like it can be done in O( n ) time and O(1) space. The problem really seems to be asking how to do the merge step of mergesort but in-place. If it was possible, wouldn't mergesort be implemented that way? I am unable to convince myself though and need

Median of Medians space complexity

纵饮孤独 提交于 2019-12-12 16:55:36
问题 I implemented an nth_number selection algorithm using Medians of Medians. On wikipedia, it states that it's space complexity is O(1) I had to store the medians in a temporary array in order to find the median amongst those medians. How would you be able to do it without using any extra memory? If it does not count as increasing its space complexity, please explain. function nth_number(v, n) { var start = 0; var end = v.length - 1; var targetIndex = n - 1; while(true) { var medians = []; /*

Space Complexity and Modifying the Data Set

我的梦境 提交于 2019-12-11 10:01:04
问题 What is the space complexity of the following algorithm? Given an array of n 32-bit signed integers, where each value is positive and less than two to the power of 30, negate all values in the array, then negate all values in the array a second time. The question arose for me out of a discussion in the comment section here: Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space I am specifically interested in different opinions and definitions. I think subtle distinctions

How to find if an algorithm takes pseudopolynomial time??

≯℡__Kan透↙ 提交于 2019-12-10 22:14:15
问题 Given a question, I try to solve it and assume that I have found an algorithm. Now I do Time Complexity analysis for that algorithm and find that it runs in polynomial time. Now How can I make sure that my algorithm runs only in polynomial time and not pseudopolynomial time? Or simply I can put up my question like this Is there any way to find if an algorithm takes pseudo polynomial time ? For example: function isPrime(n): for i from 2 to n - 1: if (n mod i) = 0, return false return true We

Find Kth largest number in single pass without storing the whole array

孤者浪人 提交于 2019-12-07 07:43:21
问题 The algo I have in mind is keep an MaxHeap of size K insert each element drop out smaller value if heap is full In the end, Kth max is the smaller of MaxHeap That will give me O(NlogK). Is there a better algorithm? I can't do quick selection, because the array can't be stored in memory. 回答1: Depending on your memory restrictions, you can use a modified version of the median-of-medians algorithm to solve the problem in O(n) time and O(k) space. The idea is as follows. Maintain an array of size

Redis Data Structure Space Requirements

半世苍凉 提交于 2019-12-06 06:31:44
问题 What is the difference in space between sorted sets and lists in redis? My guess is that sorted sets are some kind of balanced binary tree, and lists are a linked list. This means that on top of the three values that I'm encoding for each of them, key, score, value, although I'll munge together score and value for the linkedlist, the overhead is that the linkedlist needs to keep track of one other node, and the binary tree needs to keep track of two, so that the space overhead to using a