Monitoring~15 min2s · 256 MBtarget O(m) time, O(m) spacecomplexity
A monitoring system runs many request handlers. For an input of size n, handler i costs factors[i] times a growth function of n. The growth function is set by kinds[i]. The kinds are 0 constant, 1 log n, 2 n, 3 n log n, 4 n squared and 5 2 to the power n.
The team wants to know which handlers will break first as traffic grows. You will list the handlers from the slowest-growing to the fastest-growing.
Write rank_by_growth(kinds, factors).
kinds[i] is the growth kind of handler i, and factors[i] is its constant factor.Input: kinds = [2, 4, 0, 3, 2], factors = [5, 1, 900, 1, 2]
Output: [2, 0, 4, 3, 1]The kinds are 0 constant, 1 log n, 2 n, 3 n log n, 4 n squared and 5 2 to the n. The factors do not change the growth. Equal kinds keep index order.
| Growth kind | Handlers with it |
|---|---|
| 0 (constant) | 2 |
| 2 (n) | 0, 4 |
| 3 (n log n) | 3 |
| 4 (n squared) | 1 |
Reading from the slowest kind to the fastest gives [2, 0, 4, 3, 1].
Input: kinds = [5, 1, 1], factors = [1, 9, 2]
Output: [1, 2, 0]Handler 0 is exponential, so it comes last. Handlers 1 and 2 grow alike, so index order decides: [1, 2, 0].
Input: kinds = [3], factors = [7]
Output: [0]One handler is the whole ranking: [0].
0 ≤ kinds.length = factors.length ≤ 100,000 (handlers)0 ≤ kinds[i] ≤ 5 (growth kinds)1 ≤ factors[i] ≤ 1,000,000 (constant factors)Placing one handler at a time and scanning the rest costs about m × m steps, which is 10^10 at m = 100,000. There are only six growth kinds, so one pass that files each handler under its kind is enough.