views:

72

answers:

1

Saw this question recently:

Given 2 arrays, the 2nd array containing some of the elements of the 1st array, return the minimum window in the 1st array which contains all the elements of the 2nd array.

Eg : Given A={1,3,5,2,3,1} and B={1,3,2}

Output : 3 , 5 (where 3 and 5 are indices in the array A)

Even though the range 1 to 4 also contains the elements of A, the range 3 to 5 is returned Since it contains since its length is lesser than the previous range ( ( 5 - 3 ) < ( 4 - 1 ) )

I had devised a solution but I am not sure if it works correctly and also not efficient.

Give an Efficient Solution for the problem. Thanks in Advance

+4  A: 

A simple solution of iterating through the list.

  1. Have a left and right pointer, initially both at zero
  2. Move the right pointer forwards until [L..R] contains all the elements (or quit if right reaches the end).
  3. Move the left pointer forwards until [L..R] doesn't contain all the elements. See if [L-1..R] is shorter than the current best.

This is obviously linear time. You'll simply need to keep track of how many of each element of B is in the subarray for checking whether the subarray is a potential solution.

Pseudocode of this algorithm.

size = bestL = A.length;
needed = B.length-1;
found = 0; left=0; right=0;
counts = {}; //counts is a map of (number, count)
for(i in B) counts.put(i, 0);

//Increase right bound
while(right < size) {
    if(!counts.contains(right)) continue;
    amt = count.get(right);
    count.set(right, amt+1);
    if(amt == 0) found++;
    if(found == needed) {
        while(found == needed) {
            //Increase left bound
            if(counts.contains(left)) {
                amt = count.get(left);
                count.set(left, amt-1);
                if(amt == 1) found--;
            }
            left++;
        }
        if(right - left + 2 >= bestL) continue;
        bestL = right - left + 2;
        bestRange = [left-1, right] //inclusive
    }
}
Nabb