101 Logo
onenoughtone

Problem Statement

2 Keys Keyboard

Initially, there is only one character 'A' on the screen. You can perform two operations on this notepad for each step:

  1. Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).
  2. Paste: You can paste the characters which are copied last time.

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

Examples

Example 1:

Input: n = 3
Output: 3
Explanation: Initially, we have one character 'A'. Step 1: Copy All => 'A' Step 2: Paste => 'AA' Step 3: Paste => 'AAA'

Example 2:

Input: n = 1
Output: 0
Explanation: We already have one 'A' on the screen, so no operations are needed.

Constraints

  • 1 <= n <= 1000

Problem Breakdown

To solve this problem, we need to:

  1. This problem can be solved using dynamic programming or by finding the prime factorization of n.
  2. For the dynamic programming approach, we can define dp[i] as the minimum number of operations to get i &apos;A&apos;s on the screen.
  3. For each i, we can try to paste j copies (where j is a divisor of i) and see which one gives the minimum operations.
  4. Alternatively, we can observe that the minimum number of operations is the sum of the prime factors of n (counting duplicates).
ProblemSolutionCode
101 Logo
onenoughtone