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 ...
随机推荐
- Qt .pro文件配置大全!
避免以后的无意义重复劳动,将用过的所有的头文件库文件的配置都放在这里,以后要用的话直接copy就好. eigen3: INCLUDEPATH += \ /usr/local/include/eigen ...
- E20180709-hm
extract vt. 提取; (费力地) 拔出; 选取; 获得
- VS2010在WIN7下安装报错“下列组件安装失败”如何解决
VS2010在WIN7下安装报错“下列组件安装失败”如何解决 http://www.111cn.net/net/42/75914.htm
- ZK的选举算法
一.前言 前面学习了Zookeeper服务端的相关细节,其中对于集群启动而言,很重要的一部分就是Leader选举,接着就开始深入学习Leader选举. 二.Leader选举 2.1 Leader选举概 ...
- hdu 6319 Problem A. Ascending Rating (2018 Multi-University Training Contest 3)
#include <stdio.h> #include <iostream> #include <cstdlib> #include <cmath> # ...
- E. Cyclic Components (DFS)(Codeforces Round #479 (Div. 3))
#include <bits/stdc++.h> using namespace std; *1e5+; vector<int>p[maxn]; vector<int&g ...
- 51Nod 1873 初中的算术
大神的字符串快速幂 #include <iostream> #include <string> #include <algorithm> #include < ...
- B. Lecture Sleep( Educational Codeforces Round 41 (Rated for Div. 2))
前缀后缀和搞一搞,然后枚举一下区间,找出最大值 #include <iostream> #include <algorithm> using namespace std; ; ...
- Tinghua Data Mining 5
ID3 ID3算法倾向于分的很细的变量 C4.5加入分母为惩罚量
- ZROI 部分题目题解
ZROI 部分题目题解 335 首先发现一个性质: 对于最短的边而言,所有点的路径如果经过了这条边,那么路径的权值就是这条边的边权(废话) 那么我们把最短的边拎出来,可以发现,博物馆确定时,每个点按照 ...