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()-,mina=A[A.size()-],pos=A.size()-;
while(pa>=)
{
if(A[pa]>A[pa+])
{
pos=pa;
break;
}
pa--;
}
if(pos==A.size()-)
return A;
int pos_=pos+,maxa=-,max_pos=-;
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;
}
};

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

  1. 【leetcode】1053. Previous Permutation With One Swap

    题目如下: Given an array A of positive integers (not necessarily distinct), return the lexicographically ...

  2. Next Permutation & Previous Permutation

    Next Permutation Given a list of integers, which denote a permutation. Find the next permutation in ...

  3. LintCode "Previous Permutation"

    A reverse version of the Dictionary algorithm :) If you AC-ed "Next Permutation II", copy ...

  4. Previous Permutation

    Similar to next permutation, the steps as follow: 1) Find k in the increasing suffix such that nums[ ...

  5. lintcode:previous permutation上一个排列

    题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...

  6. 算法与数据结构基础 - 贪心(Greedy)

    贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...

  7. Weekly Contest 138

    1051. Height Checker Students are asked to stand in non-decreasing order of heights for an annual ph ...

  8. Lintcode: Previous Permuation

    Given a list of integers, which denote a permutation. Find the previous permutation in ascending ord ...

  9. c++ 有swap函数

    这是剑指offer数组中重复的数字那个题,直接使用的swap函数 class Solution { public: // Parameters: // numbers: an array of int ...

随机推荐

  1. 4 pyspark学习---RDD

    开始新的东西,其实很多操作在第二篇的时候就有所介绍啦.在这里继续学习一遍加深一下印象. 1关于RDD (1) RDD-----Resilient Distributed Dataset,弹性分布式数据 ...

  2. 使用fastadmin的页面异常模板

    1.效果图 2.修改tp异常页面的模板文件( /thinkphp/tpl/think_exception.tpl ),将文件中的内容全部替换成下面的内容然后保存即可(若发生报错,请注意语言包问题) & ...

  3. apache2.4.35 403 forbidden 解决办法

  4. HDU5113【DFS+剪枝】

    题意: n*m的矩阵 k种颜色 每种颜色有c[i]个 上下左右相邻的格子不能一样的颜色 问你有没有一种染色方法,有的话输出方案. 思路: 暴搜啊,n,m都才5,做完以后大哥的剪枝是奇偶剪枝,其实画完图 ...

  5. 强大的在线web编辑器UEditor

    UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码. UEditor在线演示地址:http://u ...

  6. unity3d easytouch教程

    http://www.taikr.com/group/6/thread/1987 说一说easytouch的简单使用方法,和移动平台上的rpg游戏一样,我们肯定也不陌生,我们经常玩游戏的时候用的都是虚 ...

  7. js中 call() ,apply(),bing()方法三者的用法和区别

    面试中经常会被问到的,或者做笔试题的时候也会有这样的问题,所以今天专门对这个问题做个总结: 先看个例子: var age = '19' var myObj = { name:'小赖', myAge:t ...

  8. JavaScript简介和发展史,JavaScript组成和开发工具-乐字节

    一.JavaScript简介 JavaScript 是一种具有面向对象能力的.解释型的程序设计语言.更具体一点,它是基于对象和事件驱动并具有相对安全性的客户端脚本语言.它的主要目的是,验证发往服务器端 ...

  9. URI URN URL的RFC参考文档

  10. C# 基础之密封类

    C#密封类 一.密封类 1. 密封类的定义 如果我们不希望自己编写的类被继承:如果有的类已经没有再被继承的必要,这时,我们可以使用sealed修饰符在类中进行声明,以达到该类不能派生其它类的目的,该类 ...