Skip to content

Latest commit

 

History

History
21 lines (19 loc) · 556 Bytes

1053.md

File metadata and controls

21 lines (19 loc) · 556 Bytes

1053. Previous Permutation With One Swap

Solution 1 (time O(n), space O(1))

class Solution(object):
    def prevPermOpt1(self, arr):
        """
        :type arr: List[int]
        :rtype: List[int]
        """
        n = len(arr)
        for i in range(n - 2, -1, -1):
            if arr[i] > arr[i + 1]:
                j = n - 1
                while arr[j] >= arr[i] or arr[j] == arr[j - 1]:
                    j -= 1
                arr[i], arr[j] = arr[j], arr[i]
                break
        return arr