If current element is greater than the last greater element , it is visible
if ( last < u ) : NEW_LINE INDENT ans += 1 NEW_LINE last = u NEW_LINE DEDENT return ans NEW_LINE def nextPermutation ( nums ) : NEW_LINE i = len ( nums ) - 2 NEW_LINE while i > - 1 : NEW_LINE if nums [ i ] < nums [ i + 1 ] : NEW_LINE INDENT j = len ( nums ) - 1 NEW_LINE while j > i : NEW_LINE INDENT if nums [ j ] > nums [ i ] : NEW_LINE INDENT break NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT nums [ i ] , nums [ j ] = nums [ j ] , nums [ i ] NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE nums [ i + 1 : ] = reversed ( nums [ i + 1 : ] ) NEW_LINE return nums NEW_LINE