How to print maximum number of A 's using given four keys | this function returns the optimal length string for N keystrokes ; The optimal string length is N when N is smaller than 7 ; An array to store result of subproblems ; Initializing the optimal lengths array for uptil 6 input strokes . ; Solve all subproblems in bottom manner ; for any keystroke n , we will need to choose between : - 1. pressing Ctrl - V once after copying the A ' s ▁ obtained ▁ by ▁ n - 3 ▁ keystrokes . ▁ ▁ 2 . ▁ pressing ▁ Ctrl - V ▁ twice ▁ after ▁ copying ▁ the ▁ A ' s obtained by n - 4 keystrokes . 3. pressing Ctrl - V thrice after copying the A 's  obtained by n-5 keystrokes. ; Driver Code ; for the rest of the array we will rely on the previous entries to compute new ones
def findoptimal ( N ) : NEW_LINE INDENT if ( N <= 6 ) : NEW_LINE INDENT return N NEW_LINE DEDENT screen = [ 0 ] * N NEW_LINE for n in range ( 1 , 7 ) : NEW_LINE INDENT screen [ n - 1 ] = n NEW_LINE DEDENT for n in range ( 7 , N + 1 ) : NEW_LINE INDENT screen [ n - 1 ] = max ( 2 * screen [ n - 4 ] , max ( 3 * screen [ n - 5 ] , 4 * screen [ n - 6 ] ) ) ; NEW_LINE DEDENT return screen [ N - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT for N in range ( 1 , 21 ) : NEW_LINE INDENT print ( " Maximum ▁ Number ▁ of ▁ A ' s ▁ with ▁ " , N , " ▁ keystrokes ▁ is ▁ " , findoptimal ( N ) ) NEW_LINE DEDENT DEDENT