big-o

Example of Big O of 2^n

China☆狼群 提交于 2020-02-26 06:23:41
问题 So I can picture what an algorithm is that has a complexity of n^c, just the number of nested for loops. for (var i = 0; i < dataset.len; i++ { for (var j = 0; j < dataset.len; j++) { //do stuff with i and j } } Log is something that splits the data set in half every time, binary search does this (not entirely sure what code for this looks like). But what is a simple example of an algorithm that is c^n or more specifically 2^n. Is O(2^n) based on loops through data? Or how data is split? Or

Why is the deque solution to the “Sliding Window Maximum” problem O(n) instead of O(nk)?

谁说我不能喝 提交于 2020-02-23 08:43:17
问题 The problem is to find the maximum in each subarray of size k in an array of length n. The brute force method is O(nk). But using a deque, the solution is supposedly O(n). However I am not convinced that it gets to O(n), in particular because of this while loop: # Remove all elements smaller than # the currently being added element # (Remove useless elements) while Qi and arr[i] >= arr[Qi[-1]] : Qi.pop() which is inside of a for loop from k to n. Couldn't this technically run up to k times

lovely-lucky-lambs last test case not passing

[亡魂溺海] 提交于 2020-02-23 07:18:21
问题 I am being very late to the google foobar party. I am stuck at level 2, and only the last test case is pending. But I am completely clueless of what this question is expecting in this last test case. I googled the question and looks like they've updated the test case and the constraint of lambs < 10 has been removed. Question is: Lovely Lucky LAMBs Being a henchman isn't all drudgery. Occasionally, when Commander Lambda is feeling generous, she'll hand out Lucky LAMBs (Lambda's All-purpose

Time Complexity of Nested for Loops with if Statements

你离开我真会死。 提交于 2020-02-15 13:47:40
问题 How does the if-statement of this code affect the time complexity of this code? Based off of this question: Runtime analysis, the for loop in the if statement would run n*n times. But in this code, j outpaces i so that once the second loop is run j = i^2 . What does this make the time complexity of the third for loop then? I understand that the first for loop runs n times, the second runs n^2 times, and the third runs n^2 times for a certain amount of times when triggered. So the complexity

Time Complexity of Nested for Loops with if Statements

扶醉桌前 提交于 2020-02-15 13:46:58
问题 How does the if-statement of this code affect the time complexity of this code? Based off of this question: Runtime analysis, the for loop in the if statement would run n*n times. But in this code, j outpaces i so that once the second loop is run j = i^2 . What does this make the time complexity of the third for loop then? I understand that the first for loop runs n times, the second runs n^2 times, and the third runs n^2 times for a certain amount of times when triggered. So the complexity

What is the time complexity of while loops?

不问归期 提交于 2020-02-03 04:53:04
问题 I'm trying to find the time complexity of while loops and I have no idea where to begin. I understand how to find the complexity class of for loops, but when it comes to while loops, I get completely lost. Any advice/tips on where to begin? Here is an example of a problem: x = 0; A[n] = some array of length n; while (x != A[i]) { i++; } 回答1: When you gotta do something n times, you have to do it n times. You can use a for loop, a while loop or whatever that your programming language (or

Big O of Nested Loop (int j = 0; j < i * i; ++j)

风流意气都作罢 提交于 2020-02-01 09:20:40
问题 Question 1 for (i = 0; i < n; i++) { for (j = 0; j < i * i ; j++){ } } Answer: O(n^3) At first glance, O(n^3) made sense to me, but I remember a previous problem I did: Question 2 for (int i = n; i > 0; i /= 2) { for (int j = 0; j < i; j++) { //statement } } Answer: O(n) For Question 2, the outer loop is O(log n) and the inner loop is O(2n / log n) resulting in O(n). The inner loop is O(2n / log n) because - see explanation here: Big O of Nested Loop (int j = 0; j < i; j++) Why we don't do

Master's theorem with f(n)=log n

点点圈 提交于 2020-01-28 02:45:34
问题 For master's theorem T(n) = a*T(n/b) + f(n) I am using 3 cases: If a*f(n/b) = c*f(n) for some constant c > 1 then T(n) = (n^log(b) a) If a*f(n/b) = f(n) then T(n) = (f(n) log(b) n) If a*f(n/b) = c*f(n) for some constant c < 1 then T(n) = (f(n)) But when f(n) = log n or n*log n , the value of c is dependent on value of n. How do I solve the recursive function using master's theorem? 回答1: You might find these three cases from the Wikipedia article on the Master theorem a bit more useful: Case 1

Difference between creating a dictionary by reduce on an array vs by assigning each item on iteration

老子叫甜甜 提交于 2020-01-25 03:58:11
问题 I came across a question on StackOverflow: Swift - Convert Array to Dictionary where the user wants to take the elements of an array and wants to put them in a dictionary and assign 0 to each of them. (Key as Players and value as their scores) So: var playerNames = ["Harry", "Ron", "Hermione"] becomes var scoreBoard: [String:Int] = [ "Ron":0, "Harry":0, "Hermione":0 ] This question got 2 answers: 1) Uses reduce on the array let scoreboard = playerNames.reduce(into: [String: Int]()) { $0[$1] =

Complexity analysis - take consideration of non logarithmic factor

我与影子孤独终老i 提交于 2020-01-24 19:44:31
问题 Just a quick preparation for my exam, for example I have: f(x) = 5x<sup>2</sup> + 4x * log(x) + 2 Would the big O be O(x<sup>2</sup> + x * log(x)) or should I take consideration of non-logarithmic coefficients such as 5 or 4? Likewise, consider this code for (int i = 0; i < block.length; i++) for (int j = 0; j < block.length; j++) for (int k = 0; k < 5; k++) g(); //assume worst case time performance of g() is O(1) So would the big O be O(5n 2 ) or O(n 2 )? 回答1: Complexity for f(x) = 5x^2 +