Construct an array from an existing array

Posted by Luv on Programmers See other posts from Programmers or by Luv
Published on 2012-06-22T14:52:06Z Indexed on 2012/06/22 15:24 UTC
Read the original article Hit count: 239

Filed under:
|

Given an array of integers A[1...n-1] where 'N' is the length of array A[ ]. Construct an array B such that B[i] = min(A[i], A[i+1], ..., A[i+K-1]), where K will be given.

Array B will have N-K+1 elements.

We can solve the problem using min-heaps Construct min-heap for k elements - O(k) For every next element delete the first element and insert the new element and heapify Hence Worst Case Time - O( (n-k+1)*k ) + O(k) Space - O(k)

Can we do it better?

© Programmers or respective owner

Related posts about algorithms

Related posts about heap