Use of min and max functions in C++

fmin and fmax are specifically for use with floating point numbers (hence the “f”). If you use it for ints, you may suffer performance or precision losses due to conversion, function call overhead, etc. depending on your compiler/platform. std::min and std::max are template functions (defined in header <algorithm>) which work on any type with a … Read more

Min/Max of dates in an array?

Code is tested with IE,FF,Chrome and works properly: var dates=[]; dates.push(new Date(“2011/06/25”)) dates.push(new Date(“2011/06/26”)) dates.push(new Date(“2011/06/27”)) dates.push(new Date(“2011/06/28”)) var maxDate=new Date(Math.max.apply(null,dates)); var minDate=new Date(Math.min.apply(null,dates));

Can min/max of moving window achieve in O(N)?

O(N) is possible using Deque data structure. It holds pairs (Value; Index). at every step: if (!Deque.Empty) and (Deque.Head.Index <= CurrentIndex – T) then Deque.ExtractHead; //Head is too old, it is leaving the window while (!Deque.Empty) and (Deque.Tail.Value > CurrentValue) do Deque.ExtractTail; //remove elements that have no chance to become minimum in the window Deque.AddTail(CurrentValue, … Read more

C extension:

Recent manuals say: The G++ minimum and maximum operators (‘<?’ and ‘>?’) and their compound forms (‘<?=’) and ‘>?=’) have been deprecated and are now removed from G++. Code using these operators should be modified to use std::min and std::max instead. A quick search of the past documents seems to indicate that they were removed … Read more

Getting the index of the returned max or min item using max()/min() on a list

Say that you have a list values = [3,6,1,5], and need the index of the smallest element, i.e. index_min = 2 in this case. Avoid the solution with itemgetter() presented in the other answers, and use instead index_min = min(range(len(values)), key=values.__getitem__) because it doesn’t require to import operator nor to use enumerate, and it is … Read more