import java.util.ArrayList; class FlexHistogram implements Histogram { private ArrayList _values = new ArrayList<>(); int _size; double _low, _high; private int[] _count; public FlexHistogram(int size) { _size = size; _count = null; } public int size() { return _size; } public double low(int k) { makeCount(); return _low + k * (_high - _low)/_size; } public void add(double x) { _count = null; _values.add(x); } public int count(int k) { makeCount(); return _count[k]; } private void makeCount() { if (_count == null) { _count = new int[_size]; if (_values.size() == 0) { _low = _high = 0.0; return; } _low = Double.MAX_VALUE; _high = Double.MIN_VALUE; for (double val : _values) { _low = Math.min(_low, val); _high = Math.max(_high, val); } for (double val : _values) { if (val >= _high) { _count[_count.length - 1] += 1; } else { int bucket = (int) ((val - _low)/(_high - _low) * _count.length); _count[bucket] += 1; } } } } }