Search~20 min2s · 256 MBtarget O(n + m) time, O(n) spacesorting
A search team ranks results with human raters. A rater sees two results and says which is better. That is one comparison, and it has two possible answers.
A method that asks k comparisons can end in at most 2^k different ways. A batch of n results can be in any of n! orders, where n! is 1 × 2 × ... × n. Every order needs its own ending.
For each batch size, find the smallest k that gives enough endings. That k is a floor: no rating plan can use fewer comparisons on its worst batch order.
Write fewest_comparisons(sizes).
sizes[i] is the number of results in one batch.n, find the smallest whole number k with 2^k ≥ n!.k values, in the order of sizes.Input: sizes = [3, 4]
Output: [3, 5]Three results can be in 6 orders. Four results can be in 24 orders.
| Size n | Orders n! | Smallest k | 2^k |
|---|---|---|---|
| 3 | 6 | 3 | 8 |
| 4 | 24 | 5 | 32 |
With k = 2 there are only 4 endings, which is fewer than 6. With k = 4 there are 16 endings, which is fewer than 24.
Input: sizes = [1]
Output: [0]One result has one order, and 2^0 is already 1. No comparison is needed.
Input: sizes = [10, 5, 12]
Output: [22, 7, 29]Ten results have 3,628,800 orders, and 2^22 is the first power of two above that. The answers follow the order of sizes.
0 ≤ sizes.length ≤ 100,000 (batches)1 ≤ sizes[i] ≤ 100,000 (results in one batch)At n = 100,000 the number n! has about 1.5 million binary digits. Building it for each batch costs far too much, and doing so for 100,000 batches is out of reach. The count of digits is what you need, and a sum of logarithms gives it.