1# Runtime: 486 ms (Top 14.08%) | Memory: 15.3 MB (Top 47.61%)2class Solution:34def find_max(self, i, a, n):5maxs = i + 16for j in range(n - 1, i, -1):7# if only j is greater than max and smaller than first descending element8if a[maxs] <= a[j] and a[j] < a[i]:9maxs = j10# Swap11a[i], a[maxs] = a[maxs], a[i]12return a1314def prevPermOpt1(self, arr):15n = len(arr)16for i in range(n - 1, 0, -1):17if arr[i] < arr[i - 1]:18# sending the first descending element from right to max_function19arr = self.find_max(i - 1, arr, n)20break21return arr