Engineering platform~20 min2s · 256 MBtarget O(m) time, O(1) spacecomplexity
An API gateway has several endpoints. Load tests show that the cost of endpoint i, in steps, for an input of size n is high_factor[i] × n^high_power[i] + low_factor[i] × n^low_power[i]. The low power is always below the high power.
Before a traffic launch, the team wants the endpoint that will cost the most when n is very large, namely 10^12. You will find it without computing the huge numbers.
Write slowest_endpoint(high_power, high_factor, low_power, low_factor).
i costs high_factor[i] * n ** high_power[i] + low_factor[i] * n ** low_power[i] at n = 10^12, and low_power[i] < high_power[i].n = 10^12.Input: high_power = [2, 1, 2], high_factor = [3, 500, 3], low_power = [1, 0, 0], low_factor = [7, 40, 90]
Output: 0The three costs are 3n^2 + 7n, 500n + 40 and 3n^2 + 90. Compare the terms in order: high power, its factor, low power, its factor.
| Endpoint | High power | Factor | Low power | Factor |
|---|---|---|---|---|
| 0 | 2 | 3 | 1 | 7 |
| 1 | 1 | 500 | 0 | 40 |
| 2 | 2 | 3 | 0 | 90 |
Endpoint 1 loses on the high power. Endpoints 0 and 2 tie on 2 and 3, and endpoint 0 has the higher low power, so the answer is 0.
Input: high_power = [1, 3], high_factor = [1000000, 1], low_power = [0, 0], low_factor = [1000000, 1]
Output: 1Endpoint 0 has a huge factor, but endpoint 1 has the higher power. At a very large n the power decides: 1.
Input: high_power = [4], high_factor = [9], low_power = [2], low_factor = [5]
Output: 0One endpoint is the answer: 0.
1 ≤ high_power.length ≤ 100,000 (endpoints), and the four lists have the same length1 ≤ high_power[i] ≤ 6 and 0 ≤ low_power[i] < high_power[i] (powers of n)1 ≤ high_factor[i], low_factor[i] ≤ 1,000,000 (constant factors)Comparing every endpoint with every other costs about m × m steps, which is 5 × 10^9 at m = 100,000. The costs themselves reach 10^78, which no 64-bit integer holds. Compare the terms in order and never compute the cost.