Similar to next permutation, the steps as follow:

1) Find k in the increasing suffix such that nums[k] > nums[k+1], if there is no such element, the permutation is the smallest like [0, 1, 2,... n], reverse it, we can get the previous permutaton.

2) Find the largest element in the increasing suffix which is smaller than nums[k], nums[p] < nums[k] ( p in [k+1, nums.size()), swap nums[k] with nums[p]

3) Since the suffix remains decreasing, which is the smallest suffix, instead we are looking for largest suffix, we can achieve this by reversing the suffix.

Time complexity: O(n), Space complexity: O(1)

public class PreviousP {

    private int findLargestNumSmallerThanK(List<Integer> nums, int k) {
for(int i = nums.size()-1; i > k; --i) {
if(nums.get(i) < nums.get(k)) return i;
}
return -1;
}
public List<Integer> previousP(List<Integer> nums) {
int size = nums.size();
int k = size - 2;
while(k >= 0 && nums.get(k) <= nums.get(k+1)) {
--k;
} if(k != -1) {
int p = findLargestNumSmallerThanK(nums, k);
Collections.swap(nums, k, p);
} Collections.reverse(nums.subList(k+1, size)); return nums;
} public static void main(String[] args) {
PreviousP p = new PreviousP();
System.out.println(p.previousP(Arrays.asList(6, 2, 3, 1, 4, 5)).toString().equals("[6, 2, 1, 5, 4, 3"));
}
}

Previous Permutation的更多相关文章

  1. Next Permutation & Previous Permutation

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

  2. leetcode_1053. Previous Permutation With One Swap

    1053. Previous Permutation With One Swap https://leetcode.com/problems/previous-permutation-with-one ...

  3. LintCode "Previous Permutation"

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

  4. lintcode:previous permutation上一个排列

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

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

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

  6. Lintcode: Previous Permuation

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

  7. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  8. C++STL算法速查

      非变易算法 /* 第21章 非变易算法 Non-modifying sequence operations 21.0 advance, distance 为了了解模板,先了解一下这两个迭代器操作函 ...

  9. 第23章 排序算法(包括merge等)

      第23章 排序算法  Sorting:1 sort Sort elements in range (function template)2 stable_sort Sort elements pr ...

随机推荐

  1. nginx配置 解决ajax请求跨域问题

    文章来源:http://to-u.xyz/2016/06/30/nginx-cors/ 背景描述 最近在研究RESTful API接口设计,使用的是Nginx,要实现本地http://127.0.0. ...

  2. .resources文件转为可视化.resx文件

    ResGen.exe启动闪退.--方法不对 参考:http://www.opdown.com/soft/101205.html 在cmd中启动ResGen.exe. 打开cmd:输入 C:\Windo ...

  3. CentOS 端口映射

    一个合作单位给我创建了十几台虚拟服务器做支撑.但是只给负载均衡绑定了公网IP.由于这个支撑的服务需要测试和调优,经常要往服务器上传class或者修改数据库.为了方便操作,我打算在负载均衡服务器上做端口 ...

  4. 彻底弄懂tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同

    https://blog.csdn.net/qq_22522663/article/details/78729029 1. tf.Variable与tf.get_variabletensorflow提 ...

  5. PHP面向对象之类的自动加载

    类的自动加载 含义: 当某行代码需要一个类的时候,php的内部机制可以做到“自动加载该类文件”,以满足该行需要一个类的这种需求. 什么时候需要一个类? 1,new一个对象的时候: 2,使用一个类的静态 ...

  6. 【Linux 进程】fork函数详解

    一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同, ...

  7. [leetcode]528. Random Pick with Weight按权重挑选索引

    Given an array w of positive integers, where w[i] describes the weight of index i, write a function  ...

  8. JAVA之1000字感想

    在经过Java自学的测试之后,我发现了自己所暴露的问题. 第一,   对于没有解决的问题始终没有解决,比如对文件的导入导出,在假期自学的时候就没有弄懂,现在依然没有解决,而现在没有解决,以后对于数据库 ...

  9. ARM板移植udev-126

    下载udev-126.tar.xz 下载的网址为: https://mirrors.edge.kernel.org/pub/linux/utils/kernel/hotplug/ 解压文件并且编译 # ...

  10. visual studio build and rebuild 的区别

    build 只编译发生改变的dll, (如下, 我只修改了web API,build的时候, 只有webAPI.dll发生更新) rebuild = clean + build (如下, 本项目中dl ...