Traverse from right to left
for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( A [ i ] > A [ i + 1 ] ) : NEW_LINE INDENT B [ i ] = max ( B [ i + 1 ] + 1 , B [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT B [ i ] = max ( B [ i ] , 1 ) NEW_LINE DEDENT DEDENT