time-complexity

Why is adding to or removing from the middle of a collections.deque slower than lookup there?

无人久伴 提交于 2021-02-20 06:30:47
问题 This wiki.python.org page on algorithmic complexity of some data structures says the following for a collections.deque object: A deque (double-ended queue) is represented internally as a doubly linked list. (Well, a list of arrays rather than objects, for greater efficiency.) Both ends are accessible, but even looking at the middle is slow, and adding to or removing from the middle is slower still. Two questions: 1) Is adding to the middle of a deque even possible? I don't see any method to

Why is adding to or removing from the middle of a collections.deque slower than lookup there?

做~自己de王妃 提交于 2021-02-20 06:29:53
问题 This wiki.python.org page on algorithmic complexity of some data structures says the following for a collections.deque object: A deque (double-ended queue) is represented internally as a doubly linked list. (Well, a list of arrays rather than objects, for greater efficiency.) Both ends are accessible, but even looking at the middle is slow, and adding to or removing from the middle is slower still. Two questions: 1) Is adding to the middle of a deque even possible? I don't see any method to

Why naive primality test algorithm is not polynomial

余生颓废 提交于 2021-02-19 05:34:37
问题 I would like to understand why the following naive primality test algorithm is not polynomial. IsPrime (n: an integer) Begin For i=2 to n-1 do If (n % i == 0) then return (no) EndIf EndFor return (yes) End This algorithm is said to be exponential in the size of the input n . Why is it true? And why the following sorting test algorithm is said polynomial and not exponential? IsSorted (T[n]: an array of n integer) Begin For i = 1 to n-1 do If (T[i] > T[i+1]) then return (no) EndIf EndFor return

Two egg dropping puzzle variation: unknown/infinite floors

白昼怎懂夜的黑 提交于 2021-02-17 05:46:12
问题 Preface This problem was inspired by a similar question last week on SO, that got deleted before it was clear what the real question was. I think this variation makes a nice problem that I wanted to share. Two Egg Problem A detailed definition and solution can be found here, but I will add a quick summary: Definition You are given two eggs, and access to a k -storey building. Both eggs are identical. The aim is to find out the highest floor f* from which an egg will not break when dropped out

shrink_to_fit() vs swap trick

强颜欢笑 提交于 2021-02-16 05:54:14
问题 I have a game where certain game objects spawn all at once and then despawn as they get destroyed/killed. The game objects are elements in an std::vector , and I'd like to minimize memory usage. I'm used to the swap trick, std::vector<gameObject>(gameObjectVector.begin(), gameObjectVector.end()).swap(gameObjectVector); but I noticed the inbuilt shrink_to_fit() from C++11. However, it has linear complexity while the swap trick is constant. Isn't the swap trick superior in every way? 回答1: The

Big-O for various Fibonacci Implementations

安稳与你 提交于 2021-02-15 10:26:52
问题 I just tried implementing code (in Java) for various means by which the nth term of the Fibonacci sequence can be computed and I'm hoping to verify what I've learnt. The iterative implementation is as follows: public int iterativeFibonacci(int n) { if ( n == 1 ) return 0; else if ( n == 2 ) return 1; int i = 0, j = 1, sum = 0; for ( ; (n-2) != 0; --n ) { sum = i + j; i = j; j = sum; } return sum; } The recursive implementation is as follows :- public int recursiveFibonacci(int n) { if ( n ==

Big-O for various Fibonacci Implementations

泪湿孤枕 提交于 2021-02-15 10:23:15
问题 I just tried implementing code (in Java) for various means by which the nth term of the Fibonacci sequence can be computed and I'm hoping to verify what I've learnt. The iterative implementation is as follows: public int iterativeFibonacci(int n) { if ( n == 1 ) return 0; else if ( n == 2 ) return 1; int i = 0, j = 1, sum = 0; for ( ; (n-2) != 0; --n ) { sum = i + j; i = j; j = sum; } return sum; } The recursive implementation is as follows :- public int recursiveFibonacci(int n) { if ( n ==

Big-O for various Fibonacci Implementations

三世轮回 提交于 2021-02-15 10:20:54
问题 I just tried implementing code (in Java) for various means by which the nth term of the Fibonacci sequence can be computed and I'm hoping to verify what I've learnt. The iterative implementation is as follows: public int iterativeFibonacci(int n) { if ( n == 1 ) return 0; else if ( n == 2 ) return 1; int i = 0, j = 1, sum = 0; for ( ; (n-2) != 0; --n ) { sum = i + j; i = j; j = sum; } return sum; } The recursive implementation is as follows :- public int recursiveFibonacci(int n) { if ( n ==

Time Complexity of finding all partitions of a set

99封情书 提交于 2021-02-11 12:46:41
问题 We know that this problem is np-complete, hence, no polynomial algorithm can be found for it. Also, we know that number of all partitions of a set is equal to the bell's number. I see there are few algorithms to generate all partitions of a set but couldn't find what is the time complexity to solve this problem. For example, this python code generates all partitions of a set recursively. What is the time complexity of this algorithm? Could this problem be solved with better time complexity?

Time Complexity of finding all partitions of a set

隐身守侯 提交于 2021-02-11 12:46:33
问题 We know that this problem is np-complete, hence, no polynomial algorithm can be found for it. Also, we know that number of all partitions of a set is equal to the bell's number. I see there are few algorithms to generate all partitions of a set but couldn't find what is the time complexity to solve this problem. For example, this python code generates all partitions of a set recursively. What is the time complexity of this algorithm? Could this problem be solved with better time complexity?