Optimal approximation by piecewise constant functions