<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 recursiveMinOpsHelper(self, currLength, copiedLength):
        # note that it is given that we start with only one character -> currLength = 1
        # assume that nothing was copied at first -> copiedLength = 0
        if currLength == self.targetLength:
            return 0
        if currLength > self.targetLength:
            return float('inf')

        # refenrence the dp to avoid repeated calculation
        # walk through n = 12, we will see a sub-problem repeated for state (8,4)
        if self.dp[currLength][copiedLength] != -1:
            # print('repeated subproblem')
            return self.dp[currLength][copiedLength]

        # option 1: only pasting (provided if copiedLength >= 1)
        if copiedLength >= 1:
            op1 = 1 + self.recursiveMinOpsHelper(currLength + copiedLength, copiedLength)
        else:
            op1 = float('inf')
        # option 2: copying + pasting
        op2 = 2 + self.recursiveMinOpsHelper(currLength * 2, currLength)

        # if we start with some currLength & copiedLength
        # the minimum number of operations needed to get targetLength
        currMinOps = min(op1, op2)

        # record this result in dp
        self.dp[currLength][copiedLength] = currMinOps

        return currMinOps


    def minSteps(self, n: int) -> int:
        self.targetLength = n 
        
        # we fill the 2-D dp array[i][j]
        # such that 1 <= i (currLength) <= targetLength, 1 <= j (copiedLength) <= targetLegnth // 2
        self.dp = [[-1] * (n // 2 + 1) for _ in range(n + 1)]

        return self.recursiveMinOpsHelper(1, 0)
20633