Previous Permutation
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的更多相关文章
- Next Permutation & Previous Permutation
Next Permutation Given a list of integers, which denote a permutation. Find the next permutation in ...
- leetcode_1053. Previous Permutation With One Swap
1053. Previous Permutation With One Swap https://leetcode.com/problems/previous-permutation-with-one ...
- LintCode "Previous Permutation"
A reverse version of the Dictionary algorithm :) If you AC-ed "Next Permutation II", copy ...
- lintcode:previous permutation上一个排列
题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...
- 【leetcode】1053. Previous Permutation With One Swap
题目如下: Given an array A of positive integers (not necessarily distinct), return the lexicographically ...
- Lintcode: Previous Permuation
Given a list of integers, which denote a permutation. Find the previous permutation in ascending ord ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- C++STL算法速查
非变易算法 /* 第21章 非变易算法 Non-modifying sequence operations 21.0 advance, distance 为了了解模板,先了解一下这两个迭代器操作函 ...
- 第23章 排序算法(包括merge等)
第23章 排序算法 Sorting:1 sort Sort elements in range (function template)2 stable_sort Sort elements pr ...
随机推荐
- 1.5.1、CDH 搭建Hadoop在安装之前(定制安装解决方案---使用内部包裹存储库)
使用内部包裹存储库 您可以通过托管内部存储库或手动将存储库文件复制到Cloudera Manager主机来为Cloudera Manager创建parcel存储库. 继续阅读: 托管内部包裹存储库 配 ...
- 加载 AssetBundle 的四种方法
[加载 AssetBundle 的四种方法] 1.AssetBundle.LoadFromMemoryAsync(byte[] binary, uint crc = 0); 返回AssetBundle ...
- Java拦截器+注解搭配使用
拦截器加注解可以实现登录权限验证等操作,我的应用场景也是为了验证这个,下面是简单的实现步骤 一.什么是拦截器? 拦截器是对action的一种拦截,可以在请求前后进行一些处理 可拦截controll ...
- leetcode 167 two sum II
167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...
- centos 6 和centos 7 系统下vnc配置
一. VNC 服务的大概介绍: VNC (Virtual Network Console)是虚拟网络控制台的缩写.它 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的. ...
- CentOS 端口映射
一个合作单位给我创建了十几台虚拟服务器做支撑.但是只给负载均衡绑定了公网IP.由于这个支撑的服务需要测试和调优,经常要往服务器上传class或者修改数据库.为了方便操作,我打算在负载均衡服务器上做端口 ...
- jackson java对象和json对象的互相转换
概述 Jackson框架是基于Java平台的一套数据处理工具,被称为“最好的Java Json解析器”. Jackson框架包含了3个核心库:streaming,databind,annotation ...
- linux同步机制
很早之前就接触过同步这个概念了,但是一直都很模糊,没有深入地学习了解过,近期有时间了,就花时间研习了一下<linux内核标准教程>和<深入linux设备驱动程序内核机制>这两本 ...
- 鸟哥的linux私房菜第四版
十分清晰的哟,从https://pan.baidu.com/s/1OYyGSOeR_6JDYIu0eOv_Mg下载即可,提取码9hsg
- ubuntu系列-很好用的截图工具shutter
直接在ubuntu软件市场中搜索“shutter”下载即可