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. Spring启动异常: cvc-elt.1: Cannot find the declaration of element 'beans'(转)

    Spring启动异常: cvc-elt.1: Cannot find the declaration of element 'beans' 2008-09-07 22:41 今天把在线聊天室代码改了下 ...

  2. day15模块内容

    1.生成器表达式 先说三元表达式如下 res = [i for i in range(10) if 1 > 5] 这样res就是一个列表6,7,8,9] 只要在这个基础上稍加调整,如下 方括号改 ...

  3. python--第二天总结

    一.作用域只要变量在内存中存在,则就可以使用.(栈) 二.三元运算result = 值result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为假:result ...

  4. centos 7 搭建openvpn-2.4.6

    参考:https://blog.csdn.net/weixin_42250094/article/details/80384863 http://www.startupcto.com/server-t ...

  5. JS在严格模式和非严格模式的区别

    若想在严格模式下使用JS,需要在文件的第一行加上“use strict”,在实际开发中,常常将“use strict”加入到闭包的内部 具体是: 整个脚本中使用:在这个JavaScript文件开头写' ...

  6. CentOS 查找某个软件安装路径

    1.通过rpm查看 查看软件是否安装.首先我们需要查看软件是否已经安装,或者说查看安装的软件包名称.如查找是否安装mysql 2.接着根据 rpm -ql 列出软件包安装的文件 3.综合上述以上的问题 ...

  7. appium ,selenium ,webdriver 运行原理与机制

    做测试开发的童鞋都知道,UI自动化你绕不开selenium, webdrvier, appium框架,那么这三者之间有什么关联,它们的原理是什么呢? 简单来说就是: Selenium2  将浏览器原生 ...

  8. 164. Maximum Gap (Array; sort)

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  9. H5外部浏览器直接调起微信——通过url协议 weixin:// 判断是否安装微信及启动微信

    前言: h5分享到微信,h5使用微信支付这些功能,都需要先判断是否安装微信客户端,如果已安装就启动微信,如果没有安装微信,就提示用户前去安装. 我们可以通过访问微信提供的URL协议(weixin:// ...

  10. 33 【kebernetes】一个错误的解决方案

    在安装或者重新安装kubernetes时,我碰到了这个错误: Unable to update cni config: No networks found in /etc/cni/net.d/ 这个错 ...