Loading problem...
Imagine you have a simple text editor that initially displays a single character 'X'. This specialized editor supports exactly two operations:
Duplicate Buffer: Copies the entire current text content into an internal clipboard. Note that you cannot copy a partial selection—it's all or nothing.
Insert Buffer: Pastes whatever is currently stored in the clipboard to the end of the existing text. If the clipboard is empty (i.e., you haven't performed a Duplicate Buffer operation yet), this operation does nothing.
Your goal is to determine the minimum number of operations required to produce exactly n copies of the character 'X' on the screen.
For example, if you want 6 copies, one optimal strategy would be:
Actually, a better approach for 6:
Given an integer n, return the minimum number of operations needed to achieve exactly n characters on screen.
n = 33Starting with one 'X', we perform Duplicate Buffer (clipboard = 'X'), then Insert Buffer to get 'XX', then Insert Buffer again to get 'XXX'. Total: 3 operations.
n = 10We already have exactly 1 character on the screen, so no operations are needed.
n = 65One optimal sequence: Duplicate Buffer, Insert Buffer (now 2 chars), Insert Buffer (now 3 chars), Duplicate Buffer, Insert Buffer (now 6 chars). Total: 5 operations. This is equivalent to the prime factorization of 6 = 2 + 3 = 5.
Constraints