space-complexity

Space complexity of recursive algorithm

限于喜欢 提交于 2019-12-03 10:29:34
问题 I was asked at an interview, the efficient way to solve a problem checking for pallindrome. Now i can do two things: starting from i = 0 to i = n/2 and comparing ith and n-ith character to be equal. I can use recursion to check if first and last are same and the rest of the string is a pallindrome. The second is recursive. My question is what is the difference in the space complexity of an algorithm's recursive and non-recursive versions? 回答1: Have a read at http://www.codeproject.com

Find a duplicate in array of integers

亡梦爱人 提交于 2019-12-03 05:43:39
This was an interview question. I was given an array of n+1 integers from the range [1,n] . The property of the array is that it has k (k>=1) duplicates, and each duplicate can appear more than twice. The task was to find an element of the array that occurs more than once in the best possible time and space complexity. After significant struggling, I proudly came up with O(nlogn) solution that takes O(1) space. My idea was to divide range [1,n-1] into two halves and determine which of two halves contains more elements from the input array (I was using Pigeonhole principle). The algorithm

Find a number with even number of occurrences

会有一股神秘感。 提交于 2019-12-03 02:08:28
Given an array where number of occurrences of each number is odd except one number whose number of occurrences is even. Find the number with even occurrences. e.g. 1, 1, 2, 3, 1, 2, 5, 3, 3 Output should be: 2 The below are the constraints: Numbers are not in range. Do it in-place. Required time complexity is O(N). Array may contain negative numbers. Array is not sorted. With the above constraints, all my thoughts failed: comparison based sorting, counting sort, BST's, hashing, brute-force. I am curious to know: Will XORing work here? If yes, how? This problem has been occupying my subway

Microsoft Interview: transforming a matrix

依然范特西╮ 提交于 2019-12-03 01:43:49
问题 Given a matrix of size n x m filled with 0's and 1's e.g.: 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 0 if the matrix has 1 at (i,j), fill the column j and row i with 1's i.e., we get: 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 Required complexity: O(n*m) time and O(1) space NOTE: you are not allowed to store anything except '0' or '1' in the matrix entries Above is a Microsoft Interview Question. I thought for two hours now. I have some clues but can't proceed any more. Ok. The first important part

Triplet whose sum in range (1,2)

白昼怎懂夜的黑 提交于 2019-12-03 01:04:32
问题 Given n positive real numbers in an array, find whether there exists a triplet among this set such that, the sum of the triplet is in the range (1, 2) . Do it in linear time and constant space. the array is not ordered. numbers are positive numbers are real numbers Any help would be greatly appreciated. Thanks. 回答1: The trick is to figure out a way to categorize the possible solutions and come up with a linear-time constant-space solution for each. Consider the three ranges X = (0,2/3), Y =

Algorithm with O(n log n) time and O(1) space complexity vs O(n) time and O(n) space complexity

时间秒杀一切 提交于 2019-12-02 22:59:16
I am curious to know which algorithm is better : Algorithm with O(n log n) time and O(1) space complexity Algorithm with O(n) time and O(n) space complexity Most of the algorithm which are solved in O(n long n) time and constant space can be solved in O(n) time by paying penalty in terms of space. Which algorithm is better ? How do I decide between these two parameters ? Example : Array Pair Sum Can be solved in O(n logn) time by sorting Can be solved using hash maps in O(n) time but with O(n) space Without actually testing anything (a risky move!), I'm going to claim that the O(n log n)-time,

Memory-constrained coin changing for numbers up to one billion

房东的猫 提交于 2019-12-02 18:08:25
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 = 1000, N = 10 A[] = {1,12,123,4,5,678,7,8,9,10}. The result is 10. 1000 = 678 + 123 + 123 + 12 + 12 + 12

Microsoft Interview: transforming a matrix

天涯浪子 提交于 2019-12-02 15:17:50
Given a matrix of size n x m filled with 0's and 1's e.g.: 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 0 if the matrix has 1 at (i,j), fill the column j and row i with 1's i.e., we get: 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 Required complexity: O(n*m) time and O(1) space NOTE: you are not allowed to store anything except '0' or '1' in the matrix entries Above is a Microsoft Interview Question. I thought for two hours now. I have some clues but can't proceed any more. Ok. The first important part of this question is that Even using a straight forward brute-force way , it can't be easily solved. If

Is fixed array size O(n) or O(1) in space?

陌路散爱 提交于 2019-12-02 03:40:35
Is an array declared like this: int array[M] , O(1) in space or O(n) ? where M is some fixed value. To me O(n) makes sense because it is not just a single variable but an entire array. But then i think it could be O(1) since we have a fixed size and it is not changing! If your array is of a fixed size and it does not vary with the size of the input it is O(1) since it can be expressed as c * O(1) = O(1) , with c being some constant. An example would be if you needed an array of size 5 to hold state in your algorithm that runs over a million (or some other arbitrary number) integers. The

What are the space complexities of inits and tails?

非 Y 不嫁゛ 提交于 2019-12-01 18:39:47
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 indicates otherwise. Rationale With tails , the original list can be shared. Computing tails xs simply