Problem · Dynamic Programming
Special Keyboard
Learn this problemProblem statement
You have a special keyboard with four keys:
- Key 1 prints
Aon the screen. - Key 2 performs
Ctrl-Aand selects the whole screen. - Key 3 performs
Ctrl-Cand copies the current selection into a buffer. - Key 4 performs
Ctrl-Vand appends the buffer to the screen.
Given an integer n, return the maximum number of A characters that can be produced using exactly n key presses.
Function
maxAWithSpecialKeyboard(n: int) → intExamples
Example 1
n = 7return = 9One optimal sequence is A, A, A, Ctrl-A, Ctrl-C, Ctrl-V, Ctrl-V, producing 9 copies of A.
The source shared the rule but did not include this exact sample. FastPrep added this small example so the behavior can be checked directly.