Talentd
FleetCode

651. 4 Keys Keyboard

Medium56.5% acceptance2 companiesPremium elsewhere, free here

Imagine you have a special keyboard with the following keys:

  • A: Print one 'A' on the screen.
  • Ctrl-A: Select the whole screen.
  • Ctrl-C: Copy selection to buffer.
  • Ctrl-V: Print buffer on screen appending it after what has already been printed.

Given an integer n, return the maximum number of 'A' you can print on the screen with at most n presses on the keys.

Example 1:

Input: n = 3
Output: 3
Explanation: We can at most get 3 A's on screen by pressing the following key sequence:
A, A, A

Example 2:

Input: n = 7
Output: 9
Explanation: We can at most get 9 A's on screen by pressing following key sequence:
A, A, A, Ctrl A, Ctrl C, Ctrl V, Ctrl V

Constraints:

  • 1 <= n <= 50

Asked at 2 companies

22:57
Still stuck?Read the written solution, or watch a walkthrough.
Open solution

Similar problems

2 Keys KeyboardMedium
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
3
Input
7
Your code is saved on this device.Solution