Bank app~20 min2s · 256 MBtarget O(q log cap) time, O(q) spacecomplexity
A bank runs nightly reconciliation with several methods. Each method has a growth class, and the time limit gives a budget of steps for it. The team wants to know how large an input each method can take before it misses the budget.
You will answer many such questions at once. The class tells you the steps at a given size, and the answer is the largest size whose steps fit the budget.
Write largest_n(kinds, budgets).
i has class kinds[i] and a budget of budgets[i] steps. The classes are 1 for n, 2 for n log n, 3 for n squared, 4 for n cubed and 5 for 2 to the power n.n * floor(log2 n), with 0 steps at n = 1. The steps of 2 to the n at n = 0 are 1.n with n >= 0 whose steps are at most the budget.Input: kinds = [1, 2, 3, 4, 5], budgets = [100000, 100000, 100000, 100000, 100000]
Output: [100000, 8191, 316, 46, 16]A limit of 1 millisecond at 100,000,000 steps a second gives a budget of 100,000 steps. Kinds: 1 is n, 2 is n log n, 3 is n squared, 4 is n cubed, 5 is 2 to the n.
| Kind | Largest n | Steps at that n | Steps at the next n |
|---|---|---|---|
| n | 100,000 | 100,000 | 100,001 |
| n log n | 8,191 | 98,292 | 106,496 |
| n squared | 316 | 99,856 | 100,489 |
| n cubed | 46 | 97,336 | 103,823 |
| 2 to the n | 16 | 65,536 | 131,072 |
The answers are [100000, 8191, 316, 46, 16].
Input: kinds = [3, 2], budgets = [1, 1]
Output: [1, 1]With a budget of 1, size 1 costs 1 step for n squared and 0 for n log n, and size 2 costs 4 and 2. Both answers are 1: [1, 1].
Input: kinds = [5], budgets = [1000000000000]
Output: [39]2 to the 39 is 549,755,813,888 and 2 to the 40 is 1,099,511,627,776, which is over 10^12. The answer is [39].
0 ≤ kinds.length = budgets.length ≤ 100,000 (queries)1 ≤ kinds[i] ≤ 5 (growth classes)1 ≤ budgets[i] ≤ 1,000,000,000,000 (steps)Raising n one at a time costs as many steps as the answer, which is up to 10^12 for the linear class. The steps never decrease as n grows, so halve a search range instead. Cap the range for each class so that no product overflows 64 bits.