time-complexity

What is the complexity of this code whose nested for loop repeatedly doubles its counter?

ⅰ亾dé卋堺 提交于 2019-12-18 16:21:33
问题 In the book Programming Interviews Exposed it says that the complexity of the program below is O(N), but I don't understand how this is possible. Can someone explain why this is? int var = 2; for (int i = 0; i < N; i++) { for (int j = i+1; j < N; j *= 2) { var += var; } } 回答1: You need a bit of math to see that. The inner loop iterates Θ(1 + log [N/(i+1)]) times (the 1 + is necessary since for i >= N/2 , [N/(i+1)] = 1 and the logarithm is 0, yet the loop iterates once). j takes the values (i

What is the complexity of this code whose nested for loop repeatedly doubles its counter?

不羁岁月 提交于 2019-12-18 16:21:30
问题 In the book Programming Interviews Exposed it says that the complexity of the program below is O(N), but I don't understand how this is possible. Can someone explain why this is? int var = 2; for (int i = 0; i < N; i++) { for (int j = i+1; j < N; j *= 2) { var += var; } } 回答1: You need a bit of math to see that. The inner loop iterates Θ(1 + log [N/(i+1)]) times (the 1 + is necessary since for i >= N/2 , [N/(i+1)] = 1 and the logarithm is 0, yet the loop iterates once). j takes the values (i

Longest Common Subsequence

大城市里の小女人 提交于 2019-12-18 15:55:18
问题 Consider 2 sequences X[1..m] and Y[1..n]. The memoization algorithm would compute the LCS in time O(m*n). Is there any better algorithm to find out LCS wrt time? I guess memoization done diagonally can give us O(min(m,n)) time complexity. 回答1: Gene Myers in 1986 came up with a very nice algorithm for this, described here: An O(ND) Difference Algorithm and Its Variations. This algorithm takes time proportional to the edit distance between sequences, so it is much faster when the difference is

What is the complexity of this nested triple for loop?

穿精又带淫゛_ 提交于 2019-12-18 13:21:03
问题 I have searched a bit on StackOverflow and have understood the complexity up to the point of the j-loop, which is O(n 2 ) . However with the nested addition of the k-loop, I am confused as why the complexity becomes O(n 3 ) . Can someone help me understand this? From my understanding, the i-loop have n iterations and the j-loop have 1+2+3+...+n iterations n*(n+1)/2 which is O(n 2 ) . for(i = 1; i < n; i++) { for(j = i+1; j <= n; j++) { for(k = i; k <= j; k++) { // Do something here... } } }

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

Check if 2 tree nodes are related (ancestor/descendant) in O(1) with pre-processing

前提是你 提交于 2019-12-18 11:11:44
问题 Check if 2 tree nodes are related (i.e. ancestor-descendant) solve it in O(1) time, with O(N) space (N = # of nodes) pre-processing is allowed That's it. I'll be going to my solution (approach) below. Please stop if you want to think yourself first. For a pre-processing I decided to do a pre-order (recursively go through the root first, then children) and give a label to each node. Let me explain the labels in details. Each label will consist of comma-separated natural numbers like "1,2,1,4,5

Quickly checking if set is superset of stored sets

 ̄綄美尐妖づ 提交于 2019-12-18 11:09:12
问题 The problem I am given N arrays of C booleans. I want to organize these into a datastructure that allows me to do the following operation as fast as possible: Given a new array, return true if this array is a "superset" of any of the stored arrays. With superset I mean this: A is a superset of B if A[i] is true for every i where B[i] is true. If B[i] is false, then A[i] can be anything. Or, in terms of sets instead of arrays: Store N sets (each with C possible elements) into a datastructure

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

When will the worst case of Merge Sort occur?

为君一笑 提交于 2019-12-18 09:59:34
问题 I know that worst case on mergesort is O(nlogn), the same as the average case. However, if the data are ascending or descending, this results to the minimum number of comparisons , and therefore mergesort becomes faster than random data. So my question is: What kind of input data produces the maximum number of comparisons that result to mergesort to be slower? The answer at this question says: For some sorting algorithms (e.g. quicksort), the initial order of the elements can affect the

Rough estimate of running time from Big O

假装没事ソ 提交于 2019-12-18 09:12:06
问题 If the time complexity of my program is,say O(n^2) ,How do I express running time in terms of seconds for a large value of n,10^6 ? I need a rough estimate of that to know if optimization is required or I can proceed with my code....Time Limit is 0.6 seconds The question is not about calculation of Time complexity....It's about estimation of running time from Time complexity 回答1: There is no way to calculate or estimate the running time of a some piece of code based on its Big-O rating. Big-O