https://app.laicode.io/app/problem/114
Given a list of integers representing the lengths of urls, find the 95 percentile of all lengths (95% of the urls have lengths <= returned length).
Assumptions
- The maximum length of valid url is 4096
- The list is not null and is not empty and does not contain null
Examples
- [1, 2, 3, ..., 95, 96, 97, 98, 99, 100], 95 percentile of all lengths is 95.
Medium
Array
Math
As stated in the description
- Solution 0
- sort by length ⇒ O(n log(n))
- pick urls[ceiling(95% * n) - 1]
- Solution 1
- max heap (~95%) + min heap (~5%)
- for each step ⇒ O(log(n))
- total time ⇒ O(n log(n))
- Solution 2
- key insight: max length of URLs <= 4096
- Data structure:
- similar to bucket sort
- bucket[0] = the # of URLs of length 0
- bucket[1] = the # of URLs of length 1
- …
- bucket[4096] = the # of URLs of length 4096
- Algorithm
- for each url, insert it into the bucket ⇒ O(n)
- find the min L such that sum(buckets[0...L]) >= 0.95n ⇒ O(4096) ⇒ O(1)
- What is Bucket Sort?
- Sort all URLs by length
- bucket[0]: url1 → url3 → url5 → url8
- bucket[1]: url9
- bucket[2]: url10 → url11 → url12
- …
- bucket[4096]: url1024
- url1 → url3 → url5 → url8 → url9 → url10 → url11 → url12 → … → url1024
- Sort all URLs by length
public class Solution {
public int percentile95(List<Integer> lengths) {
// Write your solution here.
if (lengths == null || lengths.isEmpty()) {
return 0;
}
// The max length of valid url is 4096
int maxLength = 4096;
int[] count = new int[maxLength + 1];
for (int length : lengths) {
count[length]++;
}
int sum = 0;
while (sum <= 0.05 * lengths.size()) {
sum += count[maxLength--];
}
return maxLength + 1;
}
}
Bucket sort all n URL lengths in the list ⇒ O(n)
An array used for bucket sort ⇒ O(n)