Monitoring~25 min2s · 256 MBtarget O(q log n) time, O(1) spaceclassic twin: lc-852, lc-162binary-search
A capacity team load-tested a service with 1, 2, 3 and more worker processes, up to n. With more workers the service handles more requests a second, but each extra worker adds no more than the one before it. Late workers can even lower the total, because they fight over the same locks and memory.
Workers are not free. The team prices one worker in requests a second: at a price p, the net value of running w workers is the requests served minus p × w. The cloud price changes during the day, so the team asks about many prices.
For each price, how many workers give the highest net value? When two counts tie, the team runs fewer workers.
Write best_worker_counts(served, prices).
served[i] is the requests a second with i + 1 workers. Each gain served[i + 1] - served[i] is at most the gain before it, so the gains never grow. A gain can be 0 or negative.p, the net value of w workers is served[w - 1] - p × w.n, with the highest net value. On a tie, the smallest such number.Input: served = [40, 70, 90, 100, 105, 106, 104], prices = [0, 8, 25, 35, 10, 1]
Output: [6, 4, 2, 1, 3, 5]The gains are 30, 20, 10, 5, 1 and -2. Take the price 8. The net values for 1 to 7 workers are 32, 54, 66, 68, 65, 58 and 48: they climb to 68 at 4 workers and then fall. One more worker is worth it exactly when its gain is above the price.
The search compares position mid with position mid + 1, where position i stands for i + 1 workers. It never builds the net values: the net value grows from mid to mid + 1 exactly when the gain served[mid + 1] - served[mid] is above the price.
| Step | lo | hi | mid | Gain at mid | Above 8? | Decision |
|---|---|---|---|---|---|---|
| 1 | 0 | 6 | 3 | 105 - 100 = 5 | no | the curve stops climbing, so hi = 3 |
| 2 | 0 | 3 | 1 | 90 - 70 = 20 | yes | still climbing, so lo = 2 |
| 3 | 2 | 3 | 2 | 100 - 90 = 10 | yes | still climbing, so lo = 3 |
| end | 3 | 3 | position 3, so 4 workers |
The price 10 equals the gain from 3 to 4 workers, so 3 and 4 workers tie at a net value of 60, and the answer is 3. The price 1 ties 5 and 6 workers at 100, and the answer is 5. At the price 0 the net value is the throughput itself, which peaks at 6 workers. The price 35 is above every gain, so 1 worker is best.
Input: served = [500], prices = [0, 7]
Output: [1, 1]With one worker count, every price gets that count.
Input: served = [10, 30, 50, 70], prices = [5, 20, 25]
Output: [4, 1, 1]Every gain is 20. At the price 5 every worker is worth adding, so the answer is all 4. At the price 20 all four counts tie at a net value of -10, so the answer is the smallest, 1.
1 ≤ served.length ≤ 100,0000 ≤ served[i] ≤ 1,000,000,000, and the gains never grow: served[i + 2] - served[i + 1] ≤ served[i + 1] - served[i]0 ≤ prices.length ≤ 100,000, and 0 ≤ prices[j] ≤ 1,000,000,000Working out the net value of every worker count for every price costs n × q steps, which is 10^10 when both are 100,000. Each price makes a different curve, so no single peak can be found once and reused. But every curve climbs and then stops climbing, so one comparison of a count with the next one shows which side the peak is on. That takes about 17 comparisons for each price.