DEV Community

Paolo Ventura
Paolo Ventura

Posted on

100 algos in 100 days (Day 33)

Relatively easy one today

How to make a temperature tracker which has getMode, GetMean, getMax, getMin methods on it.

The key being in order to optimise for speed we can use a an ahead-of-time approach: have our insert method compute and store our mean, mode, max, and min ahead of time (that is, before they're asked for). So our getter just returns the precomputed value in O(1) time.

I think the key for this in algo questions is when faced with a defined range e.g. 1...120 we know we could get all these values so why not precompute!!

Top comments (0)