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

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));

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

MIN and MAX in C

Where are MIN and MAX defined in C, if at all? They aren’t. What is the best way to implement these, as generically and type safe as possible (compiler extensions/builtins for mainstream compilers preferred). As functions. I wouldn’t use macros like #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)), especially if you plan … Read more