<p>There is only one character <code>&#39;A&#39;</code> on the screen of a notepad. You can perform one of two operations on this notepad for each step:</p>

<ul>
	<li>Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).</li>
	<li>Paste: You can paste the characters which are copied last time.</li>
</ul>

<p>Given an integer <code>n</code>, return <em>the minimum number of operations to get the character</em> <code>&#39;A&#39;</code> <em>exactly</em> <code>n</code> <em>times on the screen</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> Initially, we have one character &#39;A&#39;.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get &#39;AA&#39;.
In step 3, we use Paste operation to get &#39;AAA&#39;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 1000</code></li>
</ul>

class Solution:
    def minSteps(self, n: int) -> int:
        if n == 1:
            return 0 # Because there is already 1 A so we don't need to do anything

        """
        n = 2 -> Copy, Paste = 2
        n = 3 -> Copy, paste, paste = 3
        n = 4, copy, paste, copy, paste = 4
        n = 5, copy, paste, paste, paste, paste
                     AA       AAA  AAAA    AAAAA
               copy, paste, copy, paste
                       AA          AAAA
        """

        totalSoFar = 1
        numOperations = 1
        lastCopied = 1

        memo = {}
        def solve(n, totalSoFar, numOperations, lastCopied):
            if totalSoFar == n:
                return numOperations
            if totalSoFar > n:
                return 9999
            else:
                if totalSoFar in memo:
                    return memo[(totalSoFar, lastCopied)]
                else:
                   memo[(totalSoFar, lastCopied)] = min(solve(n, totalSoFar * 2, numOperations + 2, totalSoFar), solve(n, totalSoFar + lastCopied, numOperations + 1, lastCopied))
                   return memo[(totalSoFar, lastCopied)]
        return solve(n, totalSoFar, numOperations, lastCopied)
            
        
        
25981