time-complexity

Time complexity of zadd when value has score greater than highest score present in the targeted sorted set

丶灬走出姿态 提交于 2019-12-25 18:35:01
问题 If every value one adds to a sorted set (redis) is one with the highest score, will the time complexity be O(log(N)) for each zadd ? OR, for such edge cases, redis performs optimizations (e.g. an exception that in such cases where score is higher than the highest score in the set, simply add the value at the highest spot)? Practically, I ask because I keep a global sorted set in my app where values are zadded with time since epoch as the score. And I'm wondering whether this will still be O

why simple for loop with if condition is faster than conditional generator expression in python

大兔子大兔子 提交于 2019-12-25 15:51:54
问题 Why is this for loop with if condition in first case is more than 2 times faster than second case with a conditional generator expression? %%timeit for i in range(100000): if i < 10000: continue pass clocks at 100 loops, best of 3: 2.85 ms per loop, while using generator expression: %%timeit for i in (i for i in range(100000) if i >= 10000): pass 100 loops, best of 3: 6.03 ms per loop 回答1: First version : For each element in range: assign it to i . Second version : For each element in range:

What counts as a binary search comparison?

拟墨画扇 提交于 2019-12-25 12:36:10
问题 I'm writing a program that determines how many comparisons it takes to run a binary search algorithm for a given number and sorted array. What I don't understand is what counts as a comparison. // returns the number of comparisons it takes to find key in sorted list, array public static int binarySearch(int key, int[] array) { int left = 0; int mid; int right = array.length - 1; int i = 0; while (true) { if (left > right) { mid = -1; break; } else { mid = (left + right)/2; if (key < array[mid

What counts as a binary search comparison?

試著忘記壹切 提交于 2019-12-25 12:35:10
问题 I'm writing a program that determines how many comparisons it takes to run a binary search algorithm for a given number and sorted array. What I don't understand is what counts as a comparison. // returns the number of comparisons it takes to find key in sorted list, array public static int binarySearch(int key, int[] array) { int left = 0; int mid; int right = array.length - 1; int i = 0; while (true) { if (left > right) { mid = -1; break; } else { mid = (left + right)/2; if (key < array[mid

Time complexity of an algorithm with two loops and two recursive calls

我的未来我决定 提交于 2019-12-25 11:53:29
问题 Here is an algorithm of sorting an array with two recursive calls.I have tried to calculate its time complexity roughly but not sure.I know that two for loops will take n+n times but what to do with recursive calls and how can i calculate them? Any one can help in calculating in simple mathematical way. MySort (a[1..n] , n) { If (n <= 2) { If (first element > second) && (n = 2) then do { Interchange a[1] & a[2]; } End if } Else { Assign value to min and max by very first element of array. for

Attraction Force as coding

非 Y 不嫁゛ 提交于 2019-12-25 08:15:16
问题 There are N particles present on the X axis, where, each particle has two properties associated with it, i.e. the Position of the particle and its Attraction-Value The Attraction-Force between two particles i and j is defined as : Attraction-Force(i,j) = Distance(i,j) × MAX(Attraction_Value[i],Attraction_Value[j]) Since all of them are in a straight line, distance between any 2 particles is equal to the absolute difference between their positions. You are supposed to calculate the following :

What is the time complexity of the following algorithm?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 08:04:34
问题 for (int i = 0; i < n; i++ ) { for (int j = 0; j < n; j++ { Simple Statement } } for (int k = 0; i < n; k++ { Simple Statement Simple Statement Simple Statement Simple Statement Simple Statement } Simple Statement*25 For the nested loop, I find a time complexity of 1 (for int i = 0) + n + 1 (for i < n) + n (for i++) * [ 1 (for int j = 0 ) + 1 + n ( for j < n) + n ( for j++) ] + n (for the simple statement). This is (1+n+1+n) (1+1+n+n)+n = (2 + 2n) (2+2n)+n = 4n^2 + 9n + 4. For the following

Nested Loop Time complexity

谁说胖子不能爱 提交于 2019-12-25 06:46:31
问题 for ( i = 0; i <= n; i = i +2 ) for ( j = n; j >= i; j - - ) I know the outer loop runs for n/2 + 1 times i cant figure out how many times would the inner loop runs if we assume n = 10 the inner loop runs for 10 times when i = 0 the inner loop runs for 8 times when i = 2 and so on but i cant figure out what time complexity would that be? 回答1: Looks like average number of iterations within inner loop is n/2 . Then: (n/2 +1) * n/2 回答2: Let us assume an arbitrary n and consider an i such that 0

What is the time complexity of the recurrence T(n) = 2T(n-1) + 4

烈酒焚心 提交于 2019-12-25 02:53:44
问题 What is the time complexity of the recurrence T(n) = 2T(n-1) + 4 ? I'm having serious problems with this. I tried: T(n) = 2T(n-1)+4 = 2(2T(n-2)+4)+4 = 4T(n-2)+12= 4(2T(n-3)+4)+4 = 8T(n-3)+20 = 8(2T(n-4)+4)+4 = 16T(n-4)+36 =… T(n) = 2^kT(n-k) + (4+2^(k+1)) so it looks like T(n) = 2^n + (4+2^(n+1)) but it doesn't seem right... please help :( 回答1: Your computation are wrong. I'm assuming here that T(0)=0 T(n) = 2T(n-1)+ 4 = 2(2T(n-2)+4)+ 4 = 4T(n-2)+ 12 = 4(2T(n-3)+4)+ 12 = 8T(n-3)+ 28 = 8(2T(n

Tricky time complexity

浪尽此生 提交于 2019-12-25 01:55:33
问题 I've gotten pretty good at figuring out time complexity, but am still confused about how to determine exponential time algorithms. The algorithm below has a worst case running time O(k^n). How did they get this value? The first for-loop takes n time, the second for-loop takes k time, and the third for-loop with q is confusing. If the running time is n * k * q -something, how did they get O(k^n)? int exp(k, n) { power = 1 for i = 1 to n { newpower = 0 for j = 1 to k { for q = 1 to power {