95 Percentile
[1, 2, 3, ..., 95, 96, 97, 98, 99, 100],
95 percentile of all lengths is 95.Basic Idea:
public class Solution { public int percentile95(List<Integer> lengths) { int min = lengths.get(0), max = lengths.get(0); for (int len : lengths) { if (len < min) { min = len; } else if (len > max) { max = len; } } // create buckets for each distinct length // buckets 从 min 开始 int[] buckets = new int[max - min + 1]; for (int len : lengths) { buckets[len - min]++; } // scan buckets from left to right, // count number until larger or equal to 0.95 * size int count = 0; for (int i = 0; i < buckets.length; ++i) { count += buckets[i]; if (count >= 0.95 * lengths.size()) { return i + min; } } return -1; } }