2 Keys Keyboard

There is only one character `'A'` on the screen of a notepad. You can perform one of two operations on this notepad for each step: - **Copy All**: copy all the characters present on the screen (a partial copy is not allowed). - **Paste**: paste the characters that were copied last time. Given an integer `n`, return the minimum number of operations to get the character `'A'` exactly `n` times on the screen.

30:00

2 Keys Keyboard
medium
Free written solution
Topics
Companies

There is only one character 'A' on the screen of a notepad. You can perform one of two operations on this notepad for each step:

  • Copy All: copy all the characters present on the screen (a partial copy is not allowed).
  • Paste: paste the characters that were copied last time.

Given an integer n, return the minimum number of operations to get the character 'A' exactly n times on the screen.

Example 1:
Input: {"n":3}
Output: 3
Constraints:
  • 1≤n≤10001 \leq n \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":3}

dp[k] = fewest operations to reach k copies of A. The screen starts with one A, so dp[1] = 0.

notepad

A

dp[k] = fewest operations for k A's

1

0

2

·

3

·

amber = length being filled, blue = the divisor it builds from, green = the path to n

Variables
VariableValue
n3
DepthFunction Call
Stack empty
0/4