Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re^3: Tracking minimum of values in an array over timeby Limbic~Region (Chancellor) |
on Oct 06, 2011 at 18:27 UTC ( [id://930041]=note: print w/replies, xml ) | Need Help?? |
liverpole,
I haven't read your code but in thinking about the problem, you should never need to scan the array. The following is completely untested but it explains why I believe it is unnecessary. Update: Due to some unknown copy/paste error, the code above has been altered slightly from its original form to hopefully be correct despite still not being tested. Warning: In the CB, ambrus points out that this approach is flawed. It is not safe to reset $next_val and $next_cnt to undef. The assumption was that anytime one of the $curr_min values was incremented, it would be incremented to the $next_min and that no other value in the array could possibly be smaller. The solution is simple - put in the O(N) low water mark algorithm keeping track of the min value and count. Consideration: While you can't keep track of the minimum value without periodically scanning, you can still achieve the OP's goal as suaveant points out. Cheers - L~R
In Section
Seekers of Perl Wisdom
|
|