time-complexity

Calculate Complexity of T(n)? [duplicate]

两盒软妹~` 提交于 2020-12-13 03:48:07
问题 This question already has answers here : T(n) = T(n/10) + T(an) + n, how to solve this? (3 answers) Closed last month . Given: T(n) = T(n/10) + T(an) + n for some a (which I know nothing about its value), and that: T(n) = 1 if n < 10 . I want to check if the following is possible (for some a values, and I want to find the smallest possible a ): For every c > 0 there is n 0 > 0 such that for every n > n 0 , T(n) >= c * n Or in other words T(n)=omega(n) Any help is appreciated. 回答1: Suppose

best amortized option for insert remove element on min heap

自古美人都是妖i 提交于 2020-12-10 17:38:52
问题 I ran into an interview question recently. no additional info is given into question (maybe default implementation should be used...) n arbitrary sequences of insert and remove operations on empty min heap ( location for delete element is known ) has amortized cost of: A) insert O(log n), remove O(log n) B) insert O(log n), remove O(1) The option ( B ) is correct. We know option (A) is also can be correct but why (B) is a better option. 来源: https://stackoverflow.com/questions/65203302/best

Worst case of the Quicksort algorithm

天涯浪子 提交于 2020-12-08 06:44:20
问题 I found many implementations of quick sort algorithm, but at the end I decided to stick to this one: public static void quickSort(int array[], int start, int end) { if(end <= start || start >= end) { } else { int pivot = array[start]; int temp = 0 ; int i = start+1; for(int j = 1; j <= end; j++) { if(pivot > array[j]) { temp = array[j]; array[j] = array[i]; array[i] = temp; i++; } } array[start] = array[i-1]; array[i-1] = pivot; quickSort(array, start, i-2); quickSort(array, i, end); }} There

Worst case of the Quicksort algorithm

核能气质少年 提交于 2020-12-08 06:41:56
问题 I found many implementations of quick sort algorithm, but at the end I decided to stick to this one: public static void quickSort(int array[], int start, int end) { if(end <= start || start >= end) { } else { int pivot = array[start]; int temp = 0 ; int i = start+1; for(int j = 1; j <= end; j++) { if(pivot > array[j]) { temp = array[j]; array[j] = array[i]; array[i] = temp; i++; } } array[start] = array[i-1]; array[i-1] = pivot; quickSort(array, start, i-2); quickSort(array, i, end); }} There

Xor of all pairwise sums of integers in an array

妖精的绣舞 提交于 2020-12-03 07:37:09
问题 We have an array A for example [1, 2, 3] . I want to find the XOR of the SUM of all pairs of integers in the array. Though this can easily be done in O(n^2) (where n is the size of the array) by passing over all of the pairs, I want to improve the time complexity of the solution? Any answer that improves the time complexity would be great. E.g. for the above example array, A , the answer would be (1+2)^(1+3)^(2+3) = 2 . Since the pairwise elements are (1,2), (1,3), (2,3) , and 3 ^ 4 ^ 5 = 2 .

Xor of all pairwise sums of integers in an array

强颜欢笑 提交于 2020-12-03 07:31:01
问题 We have an array A for example [1, 2, 3] . I want to find the XOR of the SUM of all pairs of integers in the array. Though this can easily be done in O(n^2) (where n is the size of the array) by passing over all of the pairs, I want to improve the time complexity of the solution? Any answer that improves the time complexity would be great. E.g. for the above example array, A , the answer would be (1+2)^(1+3)^(2+3) = 2 . Since the pairwise elements are (1,2), (1,3), (2,3) , and 3 ^ 4 ^ 5 = 2 .

Swift Dictionary: remove time complexity

痞子三分冷 提交于 2020-11-26 08:28:04
问题 As stated by the official website, removing by key from a dictionary (or map, in other languages) is O(n) in Swift, making it a decently inefficient operation. Why isn't it O(1) if put() and get() should be O(1) based on hashing? 回答1: The source code of removeValue is: let (bucket, found) = asNative.find(key) guard found else { return nil } let isUnique = isUniquelyReferenced() return asNative.uncheckedRemove(at: bucket, isUnique: isUnique).value While the uncheckedRemove's code is: