> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# 2 Keys Keyboard Python Solution with Tests

> Tested Python solution for LeetCode 650 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 650, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/two-keys-keyboard/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 650   # by problem number
lcpy gen -s two_keys_keyboard   # by problem name
```

## Problem

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:** You can copy all the characters present on the screen (a partial copy is not allowed).
* **Paste:** You can paste the characters which are copied last time.

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

### Examples

```
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'.
```

```
Input: n = 1
Output: 0
```

### Constraints

* 1 \<= n \<= 1000

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/two_keys_keyboard/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/two_keys_keyboard/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(sqrt(n))
    # Space: O(1)
    def min_steps(self, n: int) -> int:
        # Each run of pastes multiplies the screen by a factor; copying a
        # block of size d costs d operations total, so the answer is the sum
        # of the prime factors of n.
        operations = 0
        factor = 2
        while factor * factor <= n:
            while n % factor == 0:
                operations += factor
                n //= factor
            factor += 1
        if n > 1:
            operations += n
        return operations
```

## Complexity

| Time | Space |
| - | - |
| O(sqrt(n)) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.