题目如下:

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. 快速获取批量处理Docker镜像SQL语句

    .获取批量pull语句 select concat('docker pull develop-harbor.geostar.com.cn/', t.name, ':', t1.tag) name fr ...

  2. Hypermesh中弹簧单元设置

    1D >> springs 单元类型 CBUSH1D 单元属性 PBUSH1D

  3. Linux_VMWare12 Install RHEL7

    目录 目录 前言 Install RHEL7 前言 准备考试,顺便来一波VMWare安装虚拟机的图文详解. Install RHEL7 step1. 选择自定义安装,Next step2. 版本兼容性 ...

  4. 阶段1 语言基础+高级_1-3-Java语言高级_07-网络编程_第2节 TCP协议_4_TCP通信的服务器端代码实现

    表示服务器的类是ServerSocket 启动服务器端 再启动客户端 客户端代码修改获取服务端会写的数据 先启动服务器端,再启动客户端 客户端打印: 服务器端读取:

  5. 阶段1 语言基础+高级_1-2 -面向对象和封装_14private关键字的作用及使用

    新建一个类Person代表人 创建demo03Person类去调用Person这个类 年龄设置为负数.虽然可以设置为负数.但是这个数值不合理 外部访问这个age就会报错 负数设置不进来.正数也设置不了 ...

  6. 测开之路一百二十四:flask之MVC响应过程

    MVC流程 原本的请求响应 结构: 视图: from flask import Flask, render_template app = Flask(__name__) @app.route(&quo ...

  7. Vue+Java实现在页面树形展示文件目录

    getInfo.class /** * @author Sue * @create 2019-07-16 15:06 **/ @RestController public class getInfo ...

  8. 树莓派3b折腾指南

    最近入手了树梅派3b,搭建了宿舍共享的热点和NAS,搭建透明代理科学上网的计划还没实现. 先报个价,一套折腾下来花了500大洋,树梅派3加外壳200,电源加内存卡100,显示器淘宝二手150,有线键鼠 ...

  9. source ~/.bash_profile是什么意思

    ~ 这个符号表示你的家目录,.bash_profile 是一个隐藏文件,主要是用来配置bash shell的,source -/.bash_profile 就是让这个配置文件在修改后立即生效.

  10. 最长k可重线段集问题【费用流】【优先队列Dijkstra费用流】

    题目链接 与最长K可重区间问题一样的解法,但是这道题却有很多需要注意的地方,譬如就是精度问题,一开始没考虑到sprt()里面的乘会爆了精度,然后交上去竟然是TLE,然后找的原因方向也没对,最后TLE了 ...