Looking for a dynamic programming solution

Posted by krammer on Programmers See other posts from Programmers or by krammer
Published on 2012-09-30T13:11:16Z Indexed on 2012/09/30 15:48 UTC
Read the original article Hit count: 188

Given a sequence of integers in range 1 to n. Each number can appear at most once. Let there be a symbol X in the sequence which means remove the minimum element from the list. There can be an arbitrarily number of X in the sequence. Example: 1,3,4,X,5,2,X The output is 1,2.

We need to find the best way to perform this operation.

The solution I have been thinking is:

Scan the sequence from left to right and count number of X which takes O(n) time. Perform partial sorting and find the k smallest elements (k = number of X) which takes O(n+klogk) time using median of medians.

Is there a better way to solve this problem using dynamic programming or any other way ?

© Programmers or respective owner

Related posts about algorithms

Related posts about dynamic-programming