Engineering platform~20 min2s · 256 MBtarget O(m) time, O(1) spacecomplexity · recursion
A build system runs recursive helpers over lists of files. Each helper has a shape: it calls itself once on one fewer file, once on half the files, or twice, once on each half. Every pending call keeps a frame on the call stack, and the stack has a fixed size.
The team wants to know which runs fit the stack before they start. You will count the frames of each run and compare the bytes with the stack.
Write count_calls_that_fit(kinds, sizes, frame_bytes, stack_bytes).
i has shape kinds[i], sizes[i] files, and a frame of frame_bytes[i] bytes.n - 1 files and stops at 0. Shape 1 calls itself on n // 2 files and stops at 1 or fewer. Shape 2 calls itself on n // 2 files and then on n - n // 2 files, and stops at 1 or fewer.stack_bytes.Input: kinds = [0, 1, 2, 0, 1], sizes = [900, 1000000, 1000000, 1000, 0], frame_bytes = [100, 100, 100, 100, 100], stack_bytes = 100000
Output: 4Each run needs its frames times 100 bytes, and the stack is 100,000 bytes, which is 1,000 frames.
| Run | Shape | Files | Frames | Bytes | Fits? |
|---|---|---|---|---|---|
| 0 | 0 | 900 | 901 | 90,100 | yes |
| 1 | 1 | 1,000,000 | 20 | 2,000 | yes |
| 2 | 2 | 1,000,000 | 21 | 2,100 | yes |
| 3 | 0 | 1,000 | 1,001 | 100,100 | no |
| 4 | 1 | 0 | 1 | 100 | yes |
Four runs fit.
Input: kinds = [1, 2], sizes = [3, 3], frame_bytes = [100, 100], stack_bytes = 250
Output: 1Shape 1 on 3 files calls itself on 1 file: 2 frames, 200 bytes, which fits. Shape 2 on 3 files calls on 1 and 2 files, and the call on 2 files calls on 1 and 1. The deepest chain is 3 frames, 300 bytes, which is over 250. The answer is 1.
Input: kinds = [], sizes = [], frame_bytes = [], stack_bytes = 1000
Output: 0With no runs, none fits: 0.
0 ≤ kinds.length = sizes.length = frame_bytes.length ≤ 100,000 (runs)0 ≤ kinds[i] ≤ 2 (recursion shapes)0 ≤ sizes[i] ≤ 1,000,000,000 (files)16 ≤ frame_bytes[i] ≤ 1,000 (bytes of one frame)1 ≤ stack_bytes ≤ 1,000,000,000 (bytes of stack)Running the recursions to find their depth takes up to 10^9 calls for one run. The bytes can pass 2 × 10^9, so use 64-bit integers. The deepest chain has a formula for every shape.