I want to create such class:
public TrackMaxMin(int periodInSec)
// use current system time as time
public Add(decimal value)
// return maximum Value for the last periodInSec
public Max { get {} }
// return minimum Value for the last periodInSec
public Min { get {} }
I think I can use FIFO query to store Pair<DateTime, decimal> and on each Add call I should:
- remove “outdated” values from query. when removing “update” cached Max/Min if needed
- add new value. update Max/Min if needed
My solution is trivial and straightforward. Probably you can suggest something better?
You can use a
max-heapcalledmax, amin-heapcalledmin, and aqueuecalledq.Let
xbe an element with the propertiesx.timeandx.val.When
Addis called:Createa new elementx.Addxto all of the data structures.The running time of
AddisO(lgn).When
Maxis called:max.GetMax()The running time of
MaxisO(1).When
Minis called:min.GetMin()The running time of
MinisO(1).For the maintenance of the model do this:
Have only one timer that is fired each time the period of the next to go out element in
qis due.When the period of
xis due then:q.Dequeue(),max.Delete(x),min.Delete(x)