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 ...
随机推荐
- Centos6.5 防火墙设置详解
vim /etc/sysconfig/iptables #丢弃所有进入请求 INPUT DROP [0:0] #丢弃所有转发请求 FORWARD DROP [0:0] #允许所有的output请求 O ...
- IconFont使用指南
[IconFont使用指南] 为了使用IconFont,需要先建立自己的项目. 在IconFont.cn中寻找自己喜欢的图标,加入到这个新建的项目. IconFont有三种使用方式,其中FontCla ...
- Kerberos 互信免登陆
第一步:机器加互信 将机器A的Kerberos name加到机器B的~/.k5login中,同时将机器B的Kerberos name加到机器A的~/.k5login中 例如:host/bjm6-193 ...
- 使用Fiddler查看APP的请求接口、接口参数和返回值的方法
1.下载Fiddler,然后安装成功后. 2.开启代理的设置 3.查看电脑的ip, 4.建立一个wifi局域网,什么360wifi,猎豹wifi,腾讯wifi都可以,用安装手机接入到这个局域网的wif ...
- c#实现动态加载Dll(转)
c#实现动态加载Dll 分类: .net2009-12-28 13:54 3652人阅读 评论(1) 收藏 举报 dllc#assemblynullexceptionclass 原理如下: 1.利用反 ...
- Word打开时显示*模板*,删除模板
XP系统, 找到目录 C:\Documents and Settings\Administrator\Application Data\Microsoft\Templates 删除里面的模板文件即可
- swift - 快速代码块 - 创建 tableview等一些控件 基本属性
1.创建tableview private lazy var cellId = "cellId" fileprivate lazy var tv : UITableView = { ...
- Xcode9 打包ipa(导出ipa测试包)时总是意外退出
今天用xcode9,打包ipa总是意外退出. 正处在测试阶段,所以打的也是测试包 ,路径是:Product -> Archive -> Export -> Save for Ad H ...
- webpack.dev.conf.js
var utils = require('./utils')var webpack = require('webpack')var config = require('../config') // 一 ...
- 移动端meta行大全
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable= ...