#650

2 Keys Keyboard

specialist · 725 · lc medium +31 · verified · 59.3% accepted · 4,409 likes · top 57%

play →

Description

A notepad starts with a single 'A'. Each step you may either Copy All (copy everything on screen) or Paste (paste the last copied content). Given an integer n, return the fewest total operations needed to produce exactly n copies of 'A' on screen.

Example 1:

Input: n = 3
Output: 3
Explanation: Initially, we have one character 'A'.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get 'AA'.
In step 3, we use Paste operation to get 'AAA'.

Example 2:

Input: n = 1
Output: 0

Code

1
2
3