time-complexity

Understanding the time complexity of given code

China☆狼群 提交于 2019-12-25 01:49:18
问题 I was coding for a simple question on codeforces. It reads like this: Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When he comes home in the evening, Vasya takes off the used socks and throws them away. Every m-th day (at days with numbers m, 2m, 3m, ...) mom buys a pair of socks to Vasya. She does it late in the evening, so that Vasya cannot put on a new pair of socks before the next day. How many consecutive days pass

build heap time complexity worst case vs upper bound / tight upper bound

风格不统一 提交于 2019-12-25 01:32:41
问题 HERE it is said that the worst case time complexity of building a heap is O(nlogn) but upper bound is O(n). How is a upper bound different from worst case time complexity and when one makes more sense over other. And is tight upper bound any different ? 回答1: Building a heap (also called Heapify ) is always O(n), regardless of the input distribution or the branching factor of the heap ( binary, ternary heaps ...). You misread the provided link, it states: (emphasis is mine) Although the worst

What is the time complexity of a k-way merge?

流过昼夜 提交于 2019-12-25 00:30:30
问题 I'm trying to understand the time complexity of a k-way merge using a heap, and although there is a plethora of literature available on it, I can't find one that breaks down the analysis such that I can understand. This Wikipedia article claims that "In an O(k) preprocessing step the heap is created using the standard heapify procedure". However, heap insertion is O(log(n)) and find-min is O(1) . We start by inserting the first elements of each array into the heap. This takes ∑log(i) time, i

Flood Fill Four-Way Algorithm Complexity

限于喜欢 提交于 2019-12-24 22:08:34
问题 I've searched but I can't seem to find the complexity of the Flood Fill Algorithm (Four-way ver.). What exactly is the complexity in big O notation? 回答1: The complexity of the flood fill algorithm is proportional to the number of pixels in the filled area. So, if you have e.g. a square, and M is the number of pixels in the square and N is the length of the side of a square, then M = N^2 and the complexity is O(M) = O(N^2). By the way, this question has already been answered in a comment in

Program to fnd Time Complexity of a java program [closed]

夙愿已清 提交于 2019-12-24 19:22:09
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . I've been trying to write a simple program in java to find time complexity of a program.A program whih just searches for "for" loop or "while" loop and prints the no of iteration such as O(n) or O(2n) etc. I got the i/p program in textarea.Is there any way by which i could do the opertaion? Please

What will be the big-O of following code?

扶醉桌前 提交于 2019-12-24 18:09:12
问题 The code: int temp = 0; for (int h = 1; h <= n; h = h * 2) { for (int i = 1; i <= n; i++) { for (int j = 1; j<=n; j++) { for (int k = 1; k<i; k++) { temp++ } } } } According to my calculations, big-O is lg n * n^4 . Kindly guide. Edit: Thank you all for the replies. I understood where I was making the mistake. I really appreciate everyone who participated. 回答1: for (int h = 1; h <= n; h = h * 2) // increment is pow(2). hence executed ln(n) times { for (int i = 1; i <= n; i++) // increment is

What will be the big-O of following code?

强颜欢笑 提交于 2019-12-24 18:08:13
问题 The code: int temp = 0; for (int h = 1; h <= n; h = h * 2) { for (int i = 1; i <= n; i++) { for (int j = 1; j<=n; j++) { for (int k = 1; k<i; k++) { temp++ } } } } According to my calculations, big-O is lg n * n^4 . Kindly guide. Edit: Thank you all for the replies. I understood where I was making the mistake. I really appreciate everyone who participated. 回答1: for (int h = 1; h <= n; h = h * 2) // increment is pow(2). hence executed ln(n) times { for (int i = 1; i <= n; i++) // increment is

Javascript: Running time of search through Object?

回眸只為那壹抹淺笑 提交于 2019-12-24 15:54:28
问题 I have a JavaScript Object as: object1 = { "abc" : "def", "ghi" : "jkl" } Now, when I write object1["abc"] . Is this search a linear time search, i.e., O(n) or constant time search, i.e., O(1)? 回答1: Accessing an array/object in Javascript is an O(1) operation. 回答2: Well, I've made a simple program testing this, and it doesn't show constant access time. Most likely there are some other things involved - optimization or memory management or something, but it clearly shows dependence on amount

Boolean convolution using integer multiplication

两盒软妹~` 提交于 2019-12-24 14:16:44
问题 In algorithms presented in Bringmann16 article a Boolean convolution is suggested to use to get sumset for two sets of positive integers. In above work both sets are represented as bitmasks - indicator vectors. If represent them as polynomials in form 1 + bit(0) * x + bit(1) * x^2 + ... + bit(i) * x^(i + 1) + ... + bit(n - 1) * x^n , then indeed their product contains only those monomials, whose power is number either in first set, or in second set or in sumset. The coefficients of product

Time complexity of Math.sqrt Java

怎甘沉沦 提交于 2019-12-24 14:13:22
问题 What is time-complexity of math.sqrt implementation in Java ? Java has time-complexity implemented in some technique whose, time-complexity I am trying to determine. 回答1: In most cases, Java attempts to use the "smart-power" algorithm, which results in a time-complexity of O(log n). Smart power Algorithm Also, it appears that in different cases, you could end up with different complexities; Why is multiplied many times faster than taking the square root? 回答2: It looks like it is implemented