#44Text Buffer Replication
A text editor contains exactly one text block in its buffer. You can perform two operations:
- Copy All: Copy the entire text currently present in the buffer.
- Paste: Paste the most recently copied text at the end of the current buffer.
A partial copy is not allowed. Given an integer n, determine the minimum number of operations required to make the text buffer contain exactly n copies of the original text block. The buffer initially contains one copy.
Real-World Applications: Text Editor Buffer Duplication; Cloud Data Block Replication; Memory Buffer Expansion;
Examples
Example 1
Input: n = 3
Output: 3
Explanation: Initially: X, Operations: Copy All → X, Paste → XX, Paste → XXX. Total operations = 3
Example 2
Input: n = 1
Output: 0
Explanation: The buffer already contains exactly one copy of the original text block.
Example 3
Input: n = 6
Output: 9
Explanation: Explanation: X → XX→ XXXX→ XXXXXXXXX. Each multiplication needs Copy + Paste: 1 → 2 → 4 → 9, 2 + 2 + 2 = 6 operations
Constraints
- 1 <= n <= 1000
- The buffer initially contains exactly one text block.
- A Copy All operation copies the complete current buffer.
- Partial copying is not allowed.
- A Paste operation appends the complete copied buffer.
- Paste cannot be performed before a Copy All operation.
- The final buffer must contain exactly n copies.
- Return the minimum number of operations.