#NEWLINE# def numSquares( self , n ) : #NEWLINE# #INDENT# '\n        static dp\n        F_i = min(F_{i - j^2}+1, \x0corall j)\n\n        O(n), think it as a tree, cache tree O(m+n) = O(2n); rather than O(n sqrt(n))\n        backward\n        ' #NEWLINE# while ( len ( Solution . F ) <= n ) : #NEWLINE# #INDENT# i = len ( Solution . F ) #NEWLINE# Solution . F . append ( sys . maxint ) #NEWLINE# j = 1 #NEWLINE# while ( ( i - ( j * j ) ) >= 0 ) : #NEWLINE# #INDENT# Solution . F [ i ] = min ( Solution . F [ i ] , ( Solution . F [ ( i - ( j * j ) ) ] + 1 ) ) #NEWLINE# j += 1 #NEWLINE# #UNINDENT# #UNINDENT# return Solution . F [ n ]