space-complexity

Implementation of LinkedList in python __getitem__() method

雨燕双飞 提交于 2019-12-05 16:51:47
I am implementing a LinkedList in python(3.7.4) and the code of the module is below :- LinkedList.py class Node: def __init__(self,value): self.value = value self.ref = None class LinkedList(Node): def __init__(self): self.__head = None self.__cur = None self.__count = 0 def add(self,value): if self.__head is None: self.__cur = Node(value) self.__head = self.__cur else: self.__cur.ref = Node(value) self.__cur = self.__cur.ref self.__count += 1 def getList(self): temp = self.__head while temp!=None: yield temp.value temp = temp.ref def delete(self,value): temp = self.__head while temp!=None: if

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

妖精的绣舞 提交于 2019-12-05 12:43:09
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. 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 2k in memory. Fill this buffer with the first 2k elements from the array, then run the median-of-medians

Reversing a singly linked list when a block size is given

旧街凉风 提交于 2019-12-05 12:36:41
There is a singly connected linked list and a block size is given.For eg if my linked list is 1->2->3->4->5->6->7->8-NULL and my block size is 4 then reverse the first 4 elements and then the second 4 elements.The output of the problem should be 4->3->2->1->8->7->6->5-NULL I was thinking of dividing the linked list into segments of size 4 and then reversing it. But that way I am forced to use a lot of extra nodes which is not desired at all. The space complexity should be kept to a minimum. It will be highly appreciable if someone can come with a better solution where the usage of extra nodes

PHP built in functions complexity (isAnagramOfPalindrome function)

时光毁灭记忆、已成空白 提交于 2019-12-05 09:52:05
问题 I've been googling for the past 2 hours, and I cannot find a list of php built in functions time and space complexity. I have the isAnagramOfPalindrome problem to solve with the following maximum allowed complexity: expected worst-case time complexity is O(N) expected worst-case space complexity is O(1) (not counting the storage required for input arguments). where N is the input string length. Here is my simplest solution, but I don't know if it is within the complexity limits. class

What is the best in place sorting algorithm to sort a singly linked list

徘徊边缘 提交于 2019-12-05 07:01:35
I've been reading on in place sorting algorithm to sort linked lists. As per Wikipedia Merge sort is often the best choice for sorting a linked list: in this situation it is relatively easy to implement a merge sort in such a way that it requires only Θ(1) extra space, and the slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. To my knowledge, the merge sort algorithm is not an in place sorting algorithm, and has a worst case space complexity of O(n) auxiliary. Now, with this taken

What is the Computational Complexity of Mathematica's CylindricalDecomposition

血红的双手。 提交于 2019-12-05 05:53:21
Mathematica' CylindricalDecomposition implements an algorithm known as Cylindrical Algebraic Decomposition. Wolfram MathWorld's article on Cylindrical Algebraic Decomposition says that this algorithm "becomes computationally infeasible for complicated inequalities." Can this statement be made more precise? Specifically, how does the time and space relate to the degree and number of variables of the multivariate polynomials? Does the time and space depend on other parameters? Tarski showed that for every formula including quantifiers there is always an equivalent quantifier free formula.

Binary vectors and matrix manipulation in C

ε祈祈猫儿з 提交于 2019-12-04 08:00:27
I'm trying to implement in C a data structure that would allow me to manipulate efficiently a**binary** matrix (containing only 1 or 0). I'll explain what operations I have to apply to this matrix, and would like to know what's the best possible data structure to use ? The operations are done in the field F_2 (which means 1+1 = 0 the other operations remain unchanged). I have one k * n matrix ( k < n ) called H . At most, k = 2325 and n = 3009. The operations that I will have to do over this matrix are : I will partially diagonalize it using only row swap and row additions . Once it is done, I

PHP built in functions complexity (isAnagramOfPalindrome function)

我只是一个虾纸丫 提交于 2019-12-03 22:24:52
I've been googling for the past 2 hours, and I cannot find a list of php built in functions time and space complexity. I have the isAnagramOfPalindrome problem to solve with the following maximum allowed complexity: expected worst-case time complexity is O(N) expected worst-case space complexity is O(1) (not counting the storage required for input arguments). where N is the input string length. Here is my simplest solution, but I don't know if it is within the complexity limits. class Solution { // Function to determine if the input string can make a palindrome by rearranging it static public

Find a duplicate in array of integers

孤街醉人 提交于 2019-12-03 16:24:37
问题 This was an interview question. I was given an array of n+1 integers from the range [1,n] . The property of the array is that it has k (k>=1) duplicates, and each duplicate can appear more than twice. The task was to find an element of the array that occurs more than once in the best possible time and space complexity. After significant struggling, I proudly came up with O(nlogn) solution that takes O(1) space. My idea was to divide range [1,n-1] into two halves and determine which of two

Bloom Filter Implementation

半世苍凉 提交于 2019-12-03 14:15:55
Using Bloom filter, we will be getting space optimization. The cassandra framework also has an implementation of Bloom Filter. But in detail, how is this space optimization achieved? A bloom filter isn't a "framework". It's really more like simply an algorithm. The implementation ain't very long. Here's one in Java I've tried ( .jar , source code and JavaDoc being all available): "Stand alone Java implementations of Cuckoo Hashing and Bloom Filters" (you may want to Google for this in case the following link ain't working anymore): http://lmonson.com/blog/?page_id=99 You can understand how it