2 Keys Keyboard - Problem
Imagine you're working with a simple text editor that starts with a single character 'A' on the screen. This editor has a unique constraint - it only has two keys:
- Copy All: Copies all characters currently on the screen to the clipboard (partial copying is not allowed)
- Paste: Pastes whatever was last copied
Your goal is to find the minimum number of operations needed to get exactly n copies of the character 'A' on the screen.
Example: To get 3 A's:
- Start:
A - Copy All:
A(clipboard now has 'A') - Paste:
AA - Paste:
AAA
Total: 3 operations
Input & Output
example_1.py β Simple case
$
Input:
n = 3
βΊ
Output:
3
π‘ Note:
Start with A. Copy All (1 op) β clipboard has A. Paste (1 op) β AA. Paste (1 op) β AAA. Total: 3 operations. Prime factorization: 3 is prime, so answer is 3.
example_2.py β Composite number
$
Input:
n = 9
βΊ
Output:
6
π‘ Note:
9 = 3 Γ 3, so we need sum of prime factors = 3 + 3 = 6. Sequence: A β Copy+Paste+Paste β AAA β Copy+Paste+Paste β AAAAAAAAA (9 A's).
example_3.py β Edge case
$
Input:
n = 1
βΊ
Output:
0
π‘ Note:
We already have 1 A on the screen, so no operations needed.
Visualization
Tap to expand
Understanding the Visualization
1
Factory Setup
Start with 1 item on the production line
2
Create Template
Copy All creates a template of your current production
3
Scale Production
Paste adds one template's worth of items to your line
4
Optimize Strategy
Prime factorization tells us the optimal copy-paste sequence
Key Takeaway
π― Key Insight: The minimum operations equals the sum of prime factors because each prime factor represents the most efficient copy-paste sequence for that multiplication step.
Time & Space Complexity
Time Complexity
O(βn)
We only need to check factors up to βn, as larger factors would have been found as quotients
β Linear Growth
Space Complexity
O(1)
Only uses a few variables, no additional data structures needed
β Linear Space
Constraints
- 1 β€ n β€ 1000
- n is a positive integer
- Time limit: 1 second per test case
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code