leetcode_1053. Previous Permutation With One Swap

1053. Previous Permutation With One Swap

https://leetcode.com/problems/previous-permutation-with-one-swap/

题意:Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.



解法:对于每个A,找到最右边的第一个逆序对{ A[i]>A[j] },然后将A[i]和其后小于A[i]的最大的元素交换。

class Solution
{
public:
    vector<int> prevPermOpt1(vector<int>& A)
    {
        int pa=A.size()-2,mina=A[A.size()-1],pos=A.size()-1;
        while(pa>=0)
        {
            if(A[pa]>A[pa+1])
            {
                pos=pa;
                break;
            }
            pa--;
        }
        if(pos==A.size()-1)
            return A;
        int pos_=pos+1,maxa=-1,max_pos=-1;
        while(pos_<A.size())
        {
            if(A[pos_]<A[pos]&&A[pos_]>maxa)
            {
                maxa=A[pos_];
                max_pos=pos_;
            }
            pos_++;
        }
        swap(A[pos],A[max_pos]);
        return A;
    }
};

原文地址:https://www.cnblogs.com/jasonlixuetao/p/10925923.html

时间: 2024-11-10 15:24:43

leetcode_1053. Previous Permutation With One Swap的相关文章

LeetCode 1053. Previous Permutation With One Swap

原题链接在这里:https://leetcode.com/problems/previous-permutation-with-one-swap/ 题目: Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A

Next Permutation &amp; Previous Permutation

Next Permutation Given a list of integers, which denote a permutation. Find the next permutation in ascending order. Notice The list may contains duplicate integers. Example For [1,3,2,3], the next permutation is [1,3,3,2] For [4,3,2,1], the next per

previous and next permutation

previous and next permutation 2763541 (找最后一个正序35) 2763541 (找3后面比3大的最后一个数4) 2764531 (交换3,4的位置) 2764135 (把4后面的5,3,1反转) 2764135 (找最后一个逆序41) 2764135 (找4后面比4小的最后一个数3) 2763145 (交换3,4的位置) 2763541 (把3后面的1,4,5反转) https://github.com/tongzhang1994/Facebook-Inte

lintcode-medium-Previous Permutation

Given a list of integers, which denote a permutation. Find the previous permutation in ascending order. Notice The list may contains duplicate integers. Example For [1,3,2,3], the previous permutation is [1,2,3,3] For [1,2,3,4], the previous permutat

Lintcode: Previous Permuation

Given a list of integers, which denote a permutation. Find the previous permutation in ascending order. Note The list may contains duplicate integers. Example For [1,3,2,3], the previous permutation is [1,2,3,3] For [1,2,3,4], the previous permutatio

C++STL算法速查

  非变易算法 /* 第21章 非变易算法 Non-modifying sequence operations 21.0 advance, distance 为了了解模板,先了解一下这两个迭代器操作函数 21.1 逐个容器元素for_each for_each Apply function to range (template function) 21.2 查找容器元素find find Find value in range (function template) 21.3 条件查找容器元素f

lintcode51- Previous Permutation- medium

Given a list of integers, which denote a permutation. Find the previous permutation in ascending order. Notice The list may contains duplicate integers. Example For [1,3,2,3], the previous permutation is [1,2,3,3] For [1,2,3,4], the previous permutat

N-Queens II -- leetcode

Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. 算法一 利用permutation中使用swap的思路,可以快速满足列条件的限制. 这样,在检查合法性时,只需要检查是否满足对角线的限制即可. 此算法在leetcode上的实际的执行时间为4ms. class Solution { public: i

[GeeksForGeeks] Sort an array in wave form

Given an unsorted array of integers, sort the array into a wave like array. An array arr[0...n-1] is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= .... Solution 1. O(n * logn) runtime, using sorting 1. sort the in