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 ...
随机推荐
- C专家编程
[C专家编程] 1.如果写了这样一条语句: if(3=i).那么编程器会发出“attempted assignment to literal(试图向常数赋值)”的错误信息. 所以将常量放置在==前央, ...
- linux命令清除服务器缓存
linux 服务器开了某项服务或程序后,内存占用的非常大,停止服务或关闭进程后,内存不会立即释放,需要手动释放,使用命令 echo 3 > /proc/sys/vm/drop_chaches 释 ...
- MONGO的简单语法,新手实用
window上启动方式:(简单略) mongod --dbpath E:\study_lib\mongodb\db --port=27000 show dababases; (创建配置文件的启动方式) ...
- JMeter学习(十四)JMeter函数学习(转载)
转载自 http://www.cnblogs.com/yangxia-test JMeter函数是一些能够转化在测试树中取样器或者其他配置元件的域的特殊值.一个函数的调用就像这样:${_functio ...
- python3替换文件的内容
目标:替换文件中的字符串内容 方法1:使用fileinput包 import fileinput for line in fileinput.input(“要修改的文件名", inp ...
- Android 7.0解决抓取不到https请求的问题
问题:Android7.0系统,使用fiddler不能抓取https请求 解决方法: 1.在源码res目录下新建xml目录,增加network_security_config.xml文件 (工程名/ ...
- Linux 永久PATH环境变量
在/etc/profile文件中添加变量[对所有用户生效(永久的)] 用vim在文件/etc/profile文件中增加变量,该变量将会对Linux下所有用户有效,并且是“永久的”. 例如:编辑/etc ...
- Django model 中的 class Meta 详解
Django model 中的 class Meta 详解 通过一个内嵌类 "class Meta" 给你的 model 定义元数据, 类似下面这样: class Foo(mode ...
- centos7 搭建keepalived+Nginx+tomcat
准备1台 192.168.2.224 安装Nginx,2台安装tomcat 192.168.2.222 192.168.2.223 1.安装Nginx: 上传pcre-8.36.tar.gz ...
- c++面向过程和面向对象-C++编译器是如何管理类和对象的
1,c++编译时如何区分对象调用类的方法? C++中的class从面向对象理论出发,将变量(属性)和函数(方法)集中定义在一起,用于描述现实世界中的类.从计算机的角度,程序依然由数据段(栈区内存)和代 ...