Parcel-delivery app~20 min2s · 256 MBtarget O(log pushes) or O(1) time, O(1) spacecomplexity
A parcel-delivery app stores the scans of a conveyor belt in a buffer that grows when it is full. Two growth rules are being compared. One doubles the room. The other adds a fixed number of slots each time.
Every scan costs one write, and every resize copies every scan already in the buffer. The team wants the total work of a run, to see which rule scales.
Write total_work(pushes, start, step).
start scans. pushes scans arrive one at a time, and each costs 1 write.step is 0 the new room is twice the old room. Otherwise it is the old room plus step.Input: pushes = 10, start = 1, step = 0
Output: 25Doubling from room for 1: the resizes copy 1, 2, 4 and 8 scans, which is 15, and the 10 writes make 25.
| Push | Buffer before | Copies | Buffer after |
|---|---|---|---|
| 2 | full at 1 | 1 | room for 2 |
| 3 | full at 2 | 2 | room for 4 |
| 5 | full at 4 | 4 | room for 8 |
| 9 | full at 8 | 8 | room for 16 |
The work is 15 + 10 = 25.
Input: pushes = 10, start = 1, step = 3
Output: 22Growing by 3 from room for 1.
| Push | Buffer before | Copies | Buffer after |
|---|---|---|---|
| 2 | full at 1 | 1 | room for 4 |
| 5 | full at 4 | 4 | room for 7 |
| 8 | full at 7 | 7 | room for 10 |
The copies are 1 + 4 + 7 = 12, and the writes are 10, so the work is 22.
Input: pushes = 0, start = 5, step = 2
Output: 0With no pushes, there is no work: 0.
0 ≤ pushes ≤ 100,000,000 (scans)1 ≤ start ≤ 1,000 (starting room)0 ≤ step ≤ 1,000 (0 means doubling)Making the pushes one at a time costs up to 10^8 steps, and counting the resizes of a fixed increment one by one can be 10^8 steps too. With a step of 1 the copies reach 5 × 10^15, so use 64-bit integers. A fixed increment needs a closed form.