题目如下:

Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.

Example 1:

Input: [3,2,1]
Output: [3,1,2]
Explanation: Swapping 2 and 1.

Example 2:

Input: [1,1,5]
Output: [1,1,5]
Explanation: This is already the smallest permutation.

Example 3:

Input: [1,9,4,6,7]
Output: [1,7,4,6,9]
Explanation: Swapping 9 and 7.

Example 4:

Input: [3,1,1,3]
Output: [1,3,1,3]
Explanation: Swapping 1 and 3.

Note:

  1. 1 <= A.length <= 10000
  2. 1 <= A[i] <= 10000

解题思路:要找出字典序小于自己的最大值,方法如下:从后往前遍历A,对于任意一个A[i],在[i+1,A.length]区间内找出比自己小的最大值,如果能找到这样的值,则这两个元素交换,交换之后的A即为字典序小于自己的最大值。怎么找出[i+1,A.length]区间内找出比自己小的最大值?可以把区间内所有的值存入有序的数组中,通过二分查找即可。

代码如下:

class Solution(object):
def prevPermOpt1(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
import bisect
dic = {}
val_list = []
for i in range(len(A)-1,-1,-1):
inx = bisect.bisect_left(val_list,A[i])
inx -= 1
if inx >= 0 and inx < len(val_list):
A[i], A[dic[val_list[inx]]] = A[dic[val_list[inx]]], A[i]
break
if A[i] not in dic:
bisect.insort_left(val_list,A[i])
dic[A[i]] = i
return A

【leetcode】1053. Previous Permutation With One Swap的更多相关文章

  1. 【LeetCode】31. Next Permutation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...

  2. 【LeetCode】31. Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  3. 【LeetCode】31. Next Permutation (2 solutions)

    Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...

  4. 【LeetCode】031. Next Permutation

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  5. 【leetcode】266. Palindrome Permutation

    原题 Given a string, determine if a permutation of the string could form a palindrome. For example, &q ...

  6. 【LeetCode】266. Palindrome Permutation 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  7. leetcode_1053. Previous Permutation With One Swap

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

  8. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  9. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

随机推荐

  1. 【洛谷T2695 桶哥的问题——吃桶】

    这是我们团队的一个题目(就是一个_rqy说很好写的题QwQ) 题目背景 @桶哥 这个题目的思路很玄学(性感_rqy在线讲解) 60 Pts 对于前面的六十分,好像很好拿,单纯的打一个模拟 唯一需要注意 ...

  2. windows传输文件到linux脚本

    安装pscp https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html cmd脚本 @echo off rem 拷贝的文件名称 se ...

  3. js 创建对象的方法

    <script> //1.字面量语法 var rectangle1 = {}; rectangle1.name="mindong"; rectangle1.width ...

  4. javascript 跳出循环比较

    for continue 跳出当前循环,继续下一个循环 break 结束循环 forEach 不能使用continue , break return/return false 跳出当前循环,在forE ...

  5. oracle 11g 数据库恢复技术 ---04 rman

    四 RMAN RMAN体系结构的主要组成部分: --1 目标数据库(target) --2 RMAN命令行客户端 --3 通道(channel) --4 快速恢复区(fast recovery are ...

  6. redis集群主从中断,报io过高 不错

    问题原因:1.由于这个集群redis操作非常频繁,1分钟操作数据达到1-2G,所有自动aof非常频繁,主从复制打包rdb也非常频繁,之前配置已经无法满足要求报异常如下6943:M 19 Jul 20: ...

  7. vts测试流程

    测试前提: 1.发货user版本 2.selinux:Enable 3.连接ADB,stay awake 4.烧录XXX申请的key 5.外网环境(ipv6) ATV9测试准备(正常准备环境+fast ...

  8. Java实验报告(一)&&第三周学习总结

    实验报告(一) 1. 打印输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其中各位数字立方和等于该数本身.例如,153是一个“水仙花数”. 源代码: public class Main { p ...

  9. Kotlin学习(2)函数

    函数声明: fun plus(a:Int,b:String):Boolean{ //fun 函数名(参数名:参数类型,参数名:参数类型):返回值类型 println(a) return true // ...

  10. 简单实现一个textarea自适应高度

    textarea自适应的实现方法很多,原理其实比较简单:监听textarea的input或者键盘事件,获取元素的scrollHeight,重置textarea元素的高度. 预览地址:textarea ...