Skip to the code
cf.
+25 XP

#8 Growing Buffer Copy Cost

medium

You're writing a log buffer for a browser extension. It starts with room for initial entries. When an entry is pushed and the buffer is full, it first grows: its capacity is multiplied by factor, and every entry already stored is copied to the new space, one copy per entry. Then the new entry is written.

Write copyCost(pushes, initial, factor) that returns [copies, capacity]: the total number of entry copies after pushes pushes into an empty buffer, and the capacity at the end.

Example 1

Input: pushes = 9, initial = 1, factor = 2
Output: [15,16]

The buffer grows at sizes 1, 2, 4, and 8, copying 1 + 2 + 4 + 8 = 15 entries, and ends with room for 16.

Example 2

Input: pushes = 10, initial = 4, factor = 3
Output: [4,12]

It grows once, from 4 to 12, when the fifth entry arrives, copying 4 entries.

Constraints

  • 0 <= pushes <= 10^6
  • 1 <= initial <= 1000
  • 2 <= factor <= 10
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).