space-complexity

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

What is the time complexity and space complexity of array[::-1]

笑着哭i 提交于 2020-02-04 05:19:05
问题 When reverse a list in Python, I usually use the array[::-1] for reversing and I know that a more common way might swap from two sides of the list. But I'm not sure the difference between these two solutions such as time complexity and space complexity. Code for this two methods below: def reverse(array): array[:] = array[::-1] def reverse(array): start, end = 0, len(array)-1 while start < end: array[start], array[end] = array[end], array[start] start += 1 end -= 1 回答1: In C python, assuming

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

What is the best in place sorting algorithm to sort a singly linked list

て烟熏妆下的殇ゞ 提交于 2020-01-13 09:28:07
问题 I've been reading on in place sorting algorithm to sort linked lists. As per Wikipedia Merge sort is often the best choice for sorting a linked list: in this situation it is relatively easy to implement a merge sort in such a way that it requires only Θ(1) extra space, and the slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. To my knowledge, the merge sort algorithm is not an in

Memory-constrained coin changing for numbers up to one billion

别来无恙 提交于 2020-01-11 15:49:37
问题 I faced this problem on one training. Namely we have given N different values ( N<= 100 ). Let's name this array A[N] , for this array A we are sure that we have 1 in the array and A[i] ≤ 10 9 . Secondly we have given number S where S ≤ 10 9 . Now we have to solve classic coin problem with this values. Actually we need to find minimum number of element which will sum to exactly S . Every element from A can be used infinite number of times. Time limit: 1 sec Memory limit: 256 MB Example: S =

What are the space complexities of inits and tails?

大城市里の小女人 提交于 2020-01-11 09:23:09
问题 TL; DR After reading the passage about persistence in Okasaki's Purely Functional Data Structures and going over his illustrative examples about singly linked lists (which is how Haskell's lists are implemented), I was left wondering about the space complexities of Data.List 's inits and tails ... It seems to me that the space complexity of tails is linear in the length of its argument, and the space complexity of inits is quadratic in the length of its argument, but a simple benchmark

Reverse string time and space complexity

人走茶凉 提交于 2020-01-11 06:09:43
问题 I have written different python codes to reverse a given string. But, couldn't able to figure the which one among them is efficient. Can someone point out the differences between these algorithms using time and space complexities? def reverse_1(s): result = "" for i in s : result = i + result return result def reverse_2(s): return s[::-1] There are already some solutions out there, but I couldn't find out the time and space complexity. I would like to know how much space s[::-1] will take?