Monitoring~15 min2s · 256 MBtarget O((n + q) log n) time, O(n) spacetraversal · sorting · binary-search
A monitoring system records the response time of an app for every minute, in milliseconds. The status page shows how many minutes were slower than each of several targets.
Your team lead says: "For each target, tell me how many minutes were slower than it. We list up to 100,000 targets, and the list covers up to 100,000 minutes."
A minute is slower than a target when its time is strictly greater than the target.
Write count_slower(times, thresholds).
times[i] is the response time of minute i, in milliseconds.thresholds[j] is a target, in milliseconds.thresholds.Input: times = [120, 45, 300, 80, 45], thresholds = [100, 45, 500]
Output: [2, 3, 0]The sorted times are [45, 45, 80, 120, 300].
| Threshold | Slower minutes |
|---|---|
| 100 | 2 |
| 45 | 3 |
| 500 | 0 |
At 45, the two minutes of 45 are not slower, so 120, 300 and 80 count.
Input: times = [7], thresholds = [7, 6]
Output: [0, 1]Equal to the threshold does not count as slower.
Input: times = [10, 10, 10], thresholds = [9, 10]
Output: [3, 0]Equal response times are counted one by one.
1 ≤ times.length ≤ 100,000 (minutes)1 ≤ times[i] ≤ 100,000 (milliseconds)1 ≤ thresholds.length ≤ 100,000 (targets)0 ≤ thresholds[j] ≤ 100,000 (milliseconds)Both lists can hold 100,000 items. A scan of all the minutes for every target makes 10,000,000,000 comparisons, about 100 seconds. Sorting the times once and searching for each target makes about 3.4 million steps.