time-complexity

C, Time complexity of sigma?

白昼怎懂夜的黑 提交于 2020-08-08 05:15:29
问题 How may I find the time complexity of the following code: (Sorry for adding image, I will re-edit my question once I have access to the laptop) What I have done so far: The first loop iterates n times, the second i times and the third log(i*j) times, So after simplifying I got: Sigma from i=0 to n for i*log i + n * (Sigma from j=0 to i for log i) But why this is equal to O(n^2 log(n))? 回答1: If we look at the outermost 2 loops we observe that there are 1 + 2 + 3 ... + n - 1 iterations. Using

What is the time complexity of deleting a node in a binary tree

坚强是说给别人听的谎言 提交于 2020-08-06 14:54:06
问题 For deleting a node in the binary tree, we have to search the node. That is possible in minimum O(log N) and max O(N). Depending on the node, we have to rearrange the pointers. How do we calculate the time complexity of that. 回答1: That depends on how you're doing the deletion. The most common way involves finding the successor of the node, then replacing the node with that successor. This can be done in O(h), where h is the height of the tree. In the worst case this is O(n), but in a balanced

What is the time complexity of Javascript Array.reduce() and Array.find()?

♀尐吖头ヾ 提交于 2020-07-29 13:08:52
问题 I am trying to return an array of indexes of values that add up to a given target. I am trying to solve it the fastest way I can! Examples: sumOfTwo([1, 2, 4, 4], 8) // => [2, 3] sumOfTwo([1, 2, 3, 9], 8) // => [] So first I tried a simple brute-force. (Time complexity: O(n^2) ) function sumOfTwo(arr, target) { for (let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) { if (arr[i] + arr[j] === target) { return [i, j]; } } } return []; } Then I tried: (Time complexity:

Find number of ways to create sequence A of length n satisfying m conditions

故事扮演 提交于 2020-07-16 05:20:24
问题 Find number of ways to create sequence A of length n satisfying m conditions. This sequence A should consist of only non negative numbers. Each condition is described by three integers (i,j,k) signifying max(A[i],A[j])=k. It is guaranteed that each index of the sequence will be there in at least one condition i.e. there will be finite number of such sequences. The maximum value of n will not exceed 18 and maximum value of k will not exceed 20000. I tried it using dynamic programming but the

Sliding window maximum in O(n) time

落爺英雄遲暮 提交于 2020-07-14 07:06:50
问题 Input: listi = [9, 7, 8, 4, 6, 1, 3, 2, 5] Output: # m=3 listo = [9, 8, 8, 6, 6, 3, 5] Given a random list composed of n numbers, I need to find all of the sublists of m consequtive elements, choose the largest value from the sublist and put them in a new list. def convert(listi, m): listo = [] n = len(listi) for i in range(n-m+1): listo.append(max(listi[i:3+i])) return listo The time complexity for this implementation is O(m\^{(n-m+1)} , which is pretty bad if listi is long, is there a way

Number of non repeat divisible dividends (optimize)

痴心易碎 提交于 2020-07-05 04:39:42
问题 Is there a way to improve my algorithm to get the optimal one in terms of computing time? Given a range of non-zero dividend numbers [A,B] (1<=A,B<=10^18), and a set of divisors D = {x: 1 <= x <= 50}, I want to find the total number of non-repeating dividends in [A,B] that can be divided by any number in set D. Example 1 (doesn't take too much time) The range of dividend numbers [1,10] and the divisors are {2,3,4} list of numbers divisible for divisor 2: 2,4,6,8,10 list of numbers divisible

how to calculate the time and space complexity of this nested recursive function

喜你入骨 提交于 2020-07-03 12:58:22
问题 #include <stdio.h> int fun(int n) { if(n>100) return n-10; return fun(fun(n+11)); } int main() { int r; r=fun(95); printf("%d\n",r); return 0; } the answer says time complexity is O(n), can anyone explain how? 来源: https://stackoverflow.com/questions/62381491/how-to-calculate-the-time-and-space-complexity-of-this-nested-recursive-function

how to calculate the time and space complexity of this nested recursive function

☆樱花仙子☆ 提交于 2020-07-03 12:57:11
问题 #include <stdio.h> int fun(int n) { if(n>100) return n-10; return fun(fun(n+11)); } int main() { int r; r=fun(95); printf("%d\n",r); return 0; } the answer says time complexity is O(n), can anyone explain how? 来源: https://stackoverflow.com/questions/62381491/how-to-calculate-the-time-and-space-complexity-of-this-nested-recursive-function

Prim's Algorithm Time Complexity

假装没事ソ 提交于 2020-07-03 02:17:05
问题 I was looking at the Wikipedia entry for Prim's algorithm and I noticed that its time complexity with an adjacency matrix is O(V^2) and its time complexity with a heap and adjacency list is O(E lg(V)) where E is the number of edges and V is the number of vertices in the graph. Since Prim's algorithm is used in denser graphs, E can approach V^2, but when it does, the time complexity with a heap becomes O(V^2 lg(V)) which is greater than O(V^2). Obviously, a heap will improve performance over

Prim's Algorithm Time Complexity

99封情书 提交于 2020-07-03 02:14:37
问题 I was looking at the Wikipedia entry for Prim's algorithm and I noticed that its time complexity with an adjacency matrix is O(V^2) and its time complexity with a heap and adjacency list is O(E lg(V)) where E is the number of edges and V is the number of vertices in the graph. Since Prim's algorithm is used in denser graphs, E can approach V^2, but when it does, the time complexity with a heap becomes O(V^2 lg(V)) which is greater than O(V^2). Obviously, a heap will improve performance over