Yor are given occurrences of 3 words (say, "bangalore", "hyderabad", "google") in a file.
bangalore: 100, 130, 157, ...
hyderabad: 80, 145, 180, ....
google: 60, 139, 197, ...
[Note: occurrences are given in sorted order.]
Now, you need to find the "smallest snip" containing all these three words.
[In this case: 130("hyderabad"), 139("google"), 145("bangalore") - 15 is snippet size].
Can you generalize this for m words and length of occurrences is n.
Algorithm:
Take first element from all list and put in a array.
minsnip = INT_MAX;
while any of list have element.
do
currentsnip = max in array - min in array;
if currentsnip < minsnip
minsnip = currentsnip;
delete the min element from array.
Get second element fron deleted list.
done
bangalore: 100, 130, 157, ...
hyderabad: 80, 145, 180, ....
google: 60, 139, 197, ...
[Note: occurrences are given in sorted order.]
Now, you need to find the "smallest snip" containing all these three words.
[In this case: 130("hyderabad"), 139("google"), 145("bangalore") - 15 is snippet size].
Can you generalize this for m words and length of occurrences is n.
Algorithm:
Take first element from all list and put in a array.
minsnip = INT_MAX;
while any of list have element.
do
currentsnip = max in array - min in array;
if currentsnip < minsnip
minsnip = currentsnip;
delete the min element from array.
Get second element fron deleted list.
done
Comments
Post a Comment