complexity-theory

Is stability of std::remove and std::remove_if design fail?

我怕爱的太早我们不能终老 提交于 2019-12-05 14:27:44
问题 Recently (from one SO comment) I learned that std::remove and std:remove_if are stable. Am I wrong to think this is a terrible design choice since it prevents certain optimizations? Imagine removing the first and fifth elements of a 1M std::vector . Because of stability, we can't implement remove with swap. Instead we must shift every remaining element. :( If we weren't limited by stability we could (for RA and BD iter) practically have 2 iters, one from front, second from behind, and then

What is the time complexity of java.util.HashMap class' keySet() method?

五迷三道 提交于 2019-12-05 13:19:48
I am trying to implement a plane sweep algorithm and for this I need to know the time complexity of java.util.HashMap class' keySet() method. I suspect that it is O(n log n). Am I correct? Point of clarification: I am talking about the time complexity of the keySet() method; iterating through the returned Set will take obviously O(n) time. Actually, getting the keyset is O(1) and cheap. This is because HashMap.keyset() returns the actual KeySet object associated with the HashMap. The returned Set is not a copy of the keys, but a wrapper for the actual HashMap's state. Indeed, if you update the

What would be the time complexity of counting the number of all structurally different binary trees?

我的梦境 提交于 2019-12-05 13:02:04
Using the method presented here: http://cslibrary.stanford.edu/110/BinaryTrees.html#java 12. countTrees() Solution (Java) /** For the key values 1...numKeys, how many structurally unique binary search trees are possible that store those keys? Strategy: consider that each value could be the root. Recursively find the size of the left and right subtrees. */ public static int countTrees(int numKeys) { if (numKeys <=1) { return(1); } else { // there will be one value at the root, with whatever remains // on the left and right each forming their own subtrees. // Iterate through all the values that

algorithm complexity - what double star means

蹲街弑〆低调 提交于 2019-12-05 12:28:58
Does anybody know what means doubled-star in complexity algorithm like this O(N**3) ? I found that one in PHP's similar_text() function and do not understand it. thx ** means power. Hence, n**3 means n^3. Complexity is of the order n^3 or O(n^3) This double star is the exponentiation operator in PHP(^ operator in general for exponentiation). As per PHP manual, $a ** $b ---- Exponentiation Operator Result of raising $a to the $b'th power. Introduced in PHP 5.6. hence, here the complexity is O(n^3),i.e., O of (n raised to power 3) OR cubic complexity. It's not always easy to write mathematics

What is the space complexity of this string manipulation code?

大城市里の小女人 提交于 2019-12-05 08:13:13
This piece of code is from Cracking the Coding interview book. public static boolean isUniqueChars2(String str) { boolean[] char_set = new boolean[256]; for (int i = 0; i < str.length(); i++) { int val = str.charAt(i); if (char_set[val]) return false; char_set[val] = true; } return true; } And the author mentions that, Time complexity is O(n), where n is the length of the string, and space complexity is O(n). I understand time complexity being O(n) but I don't understand how could space complexity be O(n) My thinking: char_set will remain an array of size 256 no matter what the input (str)

Asymptotic analysis of three nested for loops

时光总嘲笑我的痴心妄想 提交于 2019-12-05 07:50:16
I want to calculate the theta complexity of this nested for loop: for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { for (int k = 0; k < j; k++) { // statement I'd say it's n^3, but I don't think this is correct, because each for loop does not go from 1 to n. I did some tests: n = 5 -> 10 10 -> 120 30 -> 4060 50 -> 19600 So it must be between n^2 and n^3. I tried the summation formula and such, but my results are way too high. Though of n^2 log(n), but that's also wrong... It is O(N^3) . The exact formula is (N*(N+1)*(N+2))/6 Using Sigma Notation is an efficient step by step

Time complexity of tuple in Python

萝らか妹 提交于 2019-12-05 07:10:01
There is similar question about hash (dictionaries) and lists, also there is a good piece of info here: http://wiki.python.org/moin/TimeComplexity But I didn't find anything about tuples. The access time for data_structure[i] for a linked list is in general O(n) for dictionary is ~ O(1) What about tuple? Is it O(n) like for a linked list or O(1) like for an array? It's O(1) for both list and tuple. They are both morally equivalent to an integer indexed array. Lists and tuples are indexable in the exact same way arrays are in other languages. A simplified explanation is that space is allocated

How is schoolbook long division an O(n^2) algorithm?

六月ゝ 毕业季﹏ 提交于 2019-12-05 06:19:32
Premise: This Wikipedia page suggests that the computational complexity of "Schoolbook" long division is O(n^2) . Deduction: Instead of taking two n-digit numbers, if I take one n-digit number and one m-digit number, then the complexity would be O(n*m) . Contradiction: Suppose you divide 100000000 (n digits) by 1000 (m digits), you get 100000, which takes six steps to arrive at. Now, if you divide 100000000 (n digits) by 10000 (m digits), you get 10000. Now this takes only five steps . Conclusion: So, it seems that the order of computation should be something like O(n/m) . Question: Who is

find all rectangular areas with certain properties in a matrix

眉间皱痕 提交于 2019-12-05 05:34:04
given an n*m matrix with the possible values of 1, 2 and null: . . . . . 1 . . . 1 . . . . . 1 . . . 2 . . . . . . . . 2 . . . 1 . . . . . 1 . . . . . . . . . . . 1 . . 2 . . 2 . . . . . . 1 I am looking for all blocks B (containing all values between (x0,y0) and (x1,y1)) that: contain at least one '1' contain no '2' are not a subset of a another block with the above properties Example: The red, green and blue area all contain an '1', no '2', and are not part of a larger area. There are of course more than 3 such blocks in this picture. I want to find all these blocks. what would be a fast way

Computing circle intersections in O( (n+s) log n)

这一生的挚爱 提交于 2019-12-05 04:52:06
I'm trying to figure out how to design an algorithm that can complete this task with a O((n+s) log n) complexity. s being the amount of intersections. I've tried searching on the internet, yet couldn't really find something. Anyway, I realise having a good data structure is key here. I am using a Red Black Tree implementation in java: TreeMap. I also use the famous(?) sweep-line algorithm to help me deal with my problem. Let me explain my setup first. I have a Scheduler. This is a PriorityQueue with my circles ordered(ascending) based on their most left coordinate. scheduler.next() basically