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()-,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的更多相关文章
- 【leetcode】1053. Previous Permutation With One Swap
题目如下: Given an array A of positive integers (not necessarily distinct), return the lexicographically ...
- Next Permutation & Previous Permutation
Next Permutation Given a list of integers, which denote a permutation. Find the next permutation in ...
- LintCode "Previous Permutation"
A reverse version of the Dictionary algorithm :) If you AC-ed "Next Permutation II", copy ...
- Previous Permutation
Similar to next permutation, the steps as follow: 1) Find k in the increasing suffix such that nums[ ...
- lintcode:previous permutation上一个排列
题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
- Weekly Contest 138
1051. Height Checker Students are asked to stand in non-decreasing order of heights for an annual ph ...
- Lintcode: Previous Permuation
Given a list of integers, which denote a permutation. Find the previous permutation in ascending ord ...
- c++ 有swap函数
这是剑指offer数组中重复的数字那个题,直接使用的swap函数 class Solution { public: // Parameters: // numbers: an array of int ...
随机推荐
- 生产环境下Flask项目目录构建
接触Flask已经有大半年了,本篇博客主要来探讨如何规范化生产环境下Flask的项目目录结构.虽然目录结构见仁见智,个人有个人的看法和习惯,但总的来说,经过很多人的实践和总结,还是有很多共同的意见和想 ...
- 技术胖Flutter第四季-19导航父子页面的跳转返回
技术胖Flutter第四季-19导航父子页面的跳转返回 博客地址: https://jspang.com/post/flutter4.html#toc-010 onPressed是当前按下的时候,按下 ...
- 4-1逻辑与运算符介绍 & 4-2逻辑或运算符介绍
后面括号内的(n++)不运算了. 4-2逻辑或运算符介绍
- Android studio 集成 Genymotion
这学期刚学android,q其内置的模拟器一开起来电脑实在卡的不要不要的了.查了一下可以在studio中集成genymotion模拟器.各方面百度最后总结了几点. 要在studio中集成genymot ...
- python3 封装之property 多态 绑定方法classmethod 与 非绑定方法 staticmethod
property 特性 什么是特性property property 是一种特殊的属性,访问它时会执行一段功能(函数),然后返回值 例如 BMI指数(bmi是计算而来的,但很明显它听起来像是一个属性而 ...
- CCF 201509-3 模板生成系统 (STL+模拟)
问题描述 成成最近在搭建一个网站,其中一些页面的部分内容来自数据库中不同的数据记录,但是页面的基本结构是相同的.例如,对于展示用户信息的页面,当用户为 Tom 时,网页的源代码是 而当用户为 Jerr ...
- LuaToC#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- WindApi2 , WindOriginalApiLibrary 突然不兼容问题
1. 在新的电脑上从tfs拉下代码后编译, windoriginalapilibrary 这个工程弹出对话框,要求转为vs2013编译,选择同意,编译成功 2.WindApi2 的Reference列 ...
- 基础篇-psql帮助命令
\? psql命令帮助 \h sql语句帮助 \?常用命令 1.默认 \d 后面不跟参数,则显示当前数据库所有的表 2. \d 表名 ,则显示这个表的定义 3.\d 索引名 ,显示索引的信息 ...
- MyBatist庖丁解牛(四)
什么是MyBatis-Spring? MyBatis-Spring就是帮助你将MyBatis代码无缝的整合到Spring中.Spring将会加载必要的sqlSessionFactory类和sessio ...