
2 Keys Keyboard (medium)
Problem Statement
You're given a notepad that initially displays a single character 'A'. You have two actions available to perform on this notepad for each step:
- Copy All: It allows you to
copy everythingon the screen. - Paste: You can
pastethe characters which arecopiedlast time.
Given an integer n, return the minimum number of operations to print the character 'A' exactly n times on the screen.
Examples
-
Example 1:
- Input:
n = 4 - Expected Output:
4 - Justification: Start with 'A', copy it (
1operation), and paste it(1operations). Now, you have 'AA'. Copy it, and paste it. Total =4operations.
- Input:
-
Example 2:
- Input:
n = 5 - Expected Output:
5 - Justification: Start with 'A', copy it, and paste it four times. Total =
5operations.
- Input:
-
Example 3:
- Input:
n = 9 - Expected Output:
6 - Justification: Start with 'A', copy and paste twice (3 operations) to get 'AAA'. Copy 'AAA' and paste it twice (3 operations) to get 'AAAAAAAAA'. Total =
6operations.
- Input:
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory