time-complexity

How to calculate the complexity of a given code

旧城冷巷雨未停 提交于 2020-01-25 10:01:09
问题 Function(int n) if(n<=2) return 1; for(i=n ; i>n/8 ; i-=n/2) for(j=n ; j>2 ; j=j/2) syso(); return Function(n/2); In order to calculate I have done the following : T(n) = T(n/2) + O(1) + 2logn T(n/2): the recursive call to the function. O(1) : the if statement. 2logn: the first "for" will run only 2 times * (the second "for") that will run logn times. ** I have assumed that the second for loop will divide the j by 2 meaning that I will have j/2^k times iterations = logn. With the same logic

Time complexity when halving loop counter twice

蹲街弑〆低调 提交于 2020-01-25 09:27:08
问题 I know the time complexity when we halve loop counter is log n . That is, if we have following loop: for(i=1 ; i<n ; i*=2) { ... } then the time complexity turns out to be log n . Shouldnt halving loop counter i again give log log n time complexity? That is for below loop for(i=1 ; i<n ; i*=4) { ... } is the time complexity log log n ? I tried some values for n : n = 2^8 = 256 i = 1,4,16,64,256 (5 times) n = 2^10 = 1024 i = 1,4,16,64,256,1024 (6 times) n = 2^16 = 65536 i = 1,4,16,64,256,1024

Time Complexity Nested Loop Inner Loop + Outer Loop

只谈情不闲聊 提交于 2020-01-24 09:31:47
问题 Can anyone explain what the time complexity is for this algorithm? for (i = 1; i <= n; i++){ for(j = 1; j <= n; j += i) { // note: not j++ printf("Iteration %d : %d\n", i, j); } } 回答1: The printf in the inner loop is called exactly ceil(n) + ceil(n/2) + ceil(n/3) + ... ceil(n/n) times. To get rid of ceil , we know that ceil(y/n) is bounded above by y/n + 1 , so we know that the number of executions is >= n + n/2 + n/3 ... n/n but is < n + 1 + n/2 + 1 + n/3 + 1 + n/4 + 1... + n/n + 1 . The

why is java taking long time initializing two dimensional arrays starting with the first dimension having a big size number?

▼魔方 西西 提交于 2020-01-24 04:01:21
问题 I have noticed that initializing 2D array like this case 1 :- int ar [] [] = new int [10000001][10] ; taking more time than initializing it like this case 2 :- int ar[] [] = new int [10] [10000001] ; in case 1 it toke time around 4000ms but in case 2 it does not exceed 100ms why there is this big gap ? 回答1: Strictly speaking, Java does not have 2D arrays: instead, it uses 1D arrays arranged into 1D arrays of arrays. In your first case, in addition to the single array of arrays, Java makes

Complexity of ISO Prolog predicates

笑着哭i 提交于 2020-01-18 04:44:13
问题 Are there any guarantees for upper bounds on the time complexity of the standard Prolog predicates? For example: is it certain that sort(+List, ?SortedList) runs in O(nlog(n)) time (n being the length of List ) in any standard compliant Prolog system? 回答1: tl;dr: No and no. Let's start with sort/2 which ideally would need n ld( n ) comparisons. Fine, but how long does one comparison take? Let's try this out: tails(Es0, [Es0|Ess]) :- Es0 = [_|Es], tails(Es, Ess). tails([],[[]]). call_time(G,T)

Java: Get the most efficient combination of a large List of objects based on a field

不想你离开。 提交于 2020-01-16 08:08:54
问题 I'm looking to maximise the number of stars given a certain budget and max limit on the combination... Example question: With a budget of 500 euro, visiting only the maximum allowed restaurants or less, dine and collect the most stars possible. I'm looking to write an efficient algorithm, that could potentially process 1 million Restaurant instances for up to 10 maxRestaurants ... Can anyone help attempt the problem? Note: This is not homework. I intentionally left the attempt empty as I don

Java: Get the most efficient combination of a large List of objects based on a field

瘦欲@ 提交于 2020-01-16 08:08:20
问题 I'm looking to maximise the number of stars given a certain budget and max limit on the combination... Example question: With a budget of 500 euro, visiting only the maximum allowed restaurants or less, dine and collect the most stars possible. I'm looking to write an efficient algorithm, that could potentially process 1 million Restaurant instances for up to 10 maxRestaurants ... Can anyone help attempt the problem? Note: This is not homework. I intentionally left the attempt empty as I don

Reducing complexity to find the absolute sum of an array elements

六月ゝ 毕业季﹏ 提交于 2020-01-15 10:13:31
问题 I got this problem on Hackerrank. https://www.hackerrank.com/challenges/playing-with-numbers/problem Given an array of integers, you must answer a number of queries. Each query consists of a single integer, x and is performed as follows: Add x to each element of the array, permanently modifying it for any future queries. Find the absolute value of each element in the array and print the sum of the absolute values on a new line. All I need to complete the following method, static int[]

Definition of space complexity

人走茶凉 提交于 2020-01-15 07:03:40
问题 By time complexity we understand algorithm's running time as a function of the size of input (number of bits needed to represent the instance in memory). Then how do we define space complexity, with regard to this observation? It obviously can't be related to the size of instance... 回答1: Space complexity can be defined in multiple ways, but the usual definition is the following. We assume that the input is stored in read-only memory somewhere, that there is a dedicated write-only memory for

Definition of space complexity

扶醉桌前 提交于 2020-01-15 07:03:34
问题 By time complexity we understand algorithm's running time as a function of the size of input (number of bits needed to represent the instance in memory). Then how do we define space complexity, with regard to this observation? It obviously can't be related to the size of instance... 回答1: Space complexity can be defined in multiple ways, but the usual definition is the following. We assume that the input is stored in read-only memory somewhere, that there is a dedicated write-only memory for