Fill table using above recursive formula . Note that the table is filled in diagonal fashion ( similar to http : goo . gl / PQqoS ) , from diagonal elements to table [ 0 ] [ n - 1 ] which is the result .
for gap in range ( n ) : NEW_LINE INDENT i = 0 ; NEW_LINE for j in range ( gap , n ) : NEW_LINE INDENT mn = min1 ( arr , i , j ) ; NEW_LINE mx = max1 ( arr , i , j ) ; NEW_LINE table [ i ] [ j ] = 0 if ( 2 * mn > mx ) else min ( table [ i ] [ j - 1 ] + 1 , table [ i + 1 ] [ j ] + 1 ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT DEDENT return table [ 0 ] [ n - 1 ] ; NEW_LINE