Ride-hailing app~20 min2s · 256 MBtarget O(q k) time, O(1) spacebacktracking
A ride-hailing planner has n pending requests. Two recursions plan with them.
The first one decides, for each request in turn, to skip it or take it, so it builds every subset. The second one builds an ordered shift: it picks an unused request, then another, and stops when k requests are placed or none are left.
Your team lead says: Before we turn them on, tell me how many calls each recursion makes, and how many shifts the second one builds.
Write choice_tree_sizes(sizes, k).
n in sizes, there are n requests.min(k, n) placed requests, and each leaf is one finished shift.n, return three numbers in order: the calls of the skip-or-take recursion, the calls of the ordering recursion, and the leaves of the ordering recursion.Input: sizes = [3], k = 2
Output: [15, 10, 6]The skip-or-take tree for 3 requests has 3 levels of decisions below the root, doubling each time: 1 + 2 + 4 + 8 = 15 calls. The ordering tree stops after 2 placed requests.
| Level | Calls on the level | Why |
|---|---|---|
| 0 | 1 | the root |
| 1 | 3 | the root, which has 3 unused requests |
| 2 | 6 | 3 calls, each with 2 unused requests |
The ordering tree has 10 calls and 6 leaves. The answer is [15, 10, 6].
Input: sizes = [1, 2], k = 5
Output: [3, 2, 1, 7, 5, 2]The ordering recursion stops when the requests run out, so k = 5 acts like k = n.
Input: sizes = [4, 5, 6], k = 3
Output: [31, 41, 24, 63, 86, 60, 127, 157, 120]Each size gives three numbers, so the list has nine numbers.
1 ≤ sizes.length ≤ 1,0001 ≤ sizes[i] ≤ 171 ≤ k ≤ 17With 17 requests the ordering recursion makes 966,858,672,404,690 calls, and the skip-or-take recursion makes 262,143. The largest answer is below 2^53, so it is exact in every language. Running the recursions is practical only for small n.