<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:
        #min number of ops to get char A exactly n times on screen
        
        #if n is one -> return 0
        if n == 1:
            return 0
        #if n is prime -> return n
        #otherwise factors?
        
        
        #2 variables in a state
        #   1. number of A's present
        #   2. buffer size
        
        DP_DIM = 1001
        MAX_OPS = DP_DIM
        dp = [[MAX_OPS] * DP_DIM for _idx in range(DP_DIM)]
        
        #keep track of states as array of tuples:
        #   (s,b) = (number of A's, buffer size)
        #always starts at (1,0):
        #   0: [(1,0)]
        #always have to use first op to copy (unless n = 1)
        #   1: [(1,1)]
        #never makes sense to copy again when s = b, wasted op
        #   2: [(2,1)]
        #now it starts to branch -> can paste again or copy again
        #   3: [(3,1),(2,2)]
        #branch again
        #   4: [(4,1),(3,3),(4,2)]
        #branch again -> this time with a repeat of state (4,4) -- don't need to explore twice since equivalent and we want minimum steps so ignore if seen again
        #   5: [(5,1),(4,4),(6,3),(6,2)]
        
        #seems like the array size will grow exponentially
        #but only are n*n combination of states and n <= 1000
        #so its fine
        
        dp[1][0] = 0
        dp[1][1] = 1
        states = [(1,1)]
        next_states = []
        for op_count in range(2, MAX_OPS):
            for str_len, buff_len in states:
                #always can append
                new_str_len = str_len + buff_len
                if new_str_len > n:
                    continue
                elif new_str_len == n:
                    return op_count
                dp[new_str_len][buff_len] = op_count
                next_states.append((new_str_len, buff_len))
                #if not already copied try copy
                if dp[str_len][str_len] > op_count:
                    dp[str_len][str_len] = op_count
                    next_states.append((str_len, str_len))
            states = next_states
            next_states = []
        #should never get here
        return -1

        
24121