Bryan R Harris wrote at Thu, 30 May 2002 01:58:19 +0200:

> I'm trying to come up with an algorithm that seems like it ought to be really easy, 
>but it's
> turning out to be pretty tough...
> 
> Basically I have three pairs of start/stop times, e.g.:
> 
>    3, 5
>    4, 10
>   15, 20
> 
> I want the total time covered by all these ranges.  I can't just say (5-3 + 10-4 + 
>20-15) because
> the 3,5 and 4,10 ranges overlap.  The desired answer for this case is 13, 3 to 10 
>and 15 to 20.  

Sorry, but 3 to 10 are 3,4,5,6,7,8,9,10 = 8 times
and 15 to 20 are 15,16,17,18,19,20 = 6 times.
So the answer should be 8+6 = 14, shouldn't ?

> I
> have the start times in two arrays, @starts and @stops.
> 
> I have to do this probably a million+ times.  Any ideas on how I could go about this?
> 

Of course, the solutions of Beau and Felix are good, TMTWTDI,
heres are very readable solution 
(but perhaps very memory reluctant :-( )

my %timings;
$timings{$_}++ for (3 .. 5, 4 .. 10, 15 .. 20);
my $total_time = scalar values %timings;


Greetings,
Janek

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to