作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/problems/orderly-queue/description/

题目描述:

A string S of lowercase letters is given. Then, we may make any number of moves.

In each move, we choose one of the first K letters (starting from the left), remove it, and place it at the end of the string.

Return the lexicographically smallest string we could have after any number of moves.

Example 1:

Input: S = "cba", K = 1
Output: "acb"
Explanation:
In the first move, we move the 1st character ("c") to the end, obtaining the string "bac".
In the second move, we move the 1st character ("b") to the end, obtaining the final result "acb".

Example 2:

Input: S = "baaca", K = 3
Output: "aaabc"
Explanation:
In the first move, we move the 1st character ("b") to the end, obtaining the string "aacab".
In the second move, we move the 3rd character ("c") to the end, obtaining the final result "aaabc".

Note:

  1. 1 <= K <= S.length <= 1000
  2. S consists of lowercase letters only.

题目大意

给了一个字符串和一个数字K,每次操作可以把字符串的前K个数字中任意抽取一个放到最后,可以做很多次操作。问,经过很多次操作之后,能构成的字母序最小的字符串是什么?

解题方法

这是一道智商题。

  1. K == 1的时候,没有什么好说的,每次只能把每个开头的字母放到最后去,进行S.length次操作之后求字母序最小的字符串。这个操作是把字符串进行了旋转。

     12345 -> 23451 -> 34512 -> 45123 -> 51234
  2. K > 1的时候,我们可以:
    1). 把字符串进行旋转(相当于K == 1
    2). 把字符串除了第一个字符的其余部分进行旋转

     012345 -> 023451 -> 034512 -> 045123 -> 051234

所以,我们可以使用步骤1里面的操作,把整个字符串最小的数字放到最前面去。然后对字符串后面的部分再旋转使后面部分最小的数字放到前面……以此类推,我们可以得到任何字符串升序排列。

当k > 1的时候,这个过程相当于冒泡排序,所以,我们可以直接排序得到结果。

时间复杂度是O(N ^ 2),空间复杂度是O(N)。

class Solution(object):
def orderlyQueue(self, S, K):
"""
:type S: str
:type K: int
:rtype: str
"""
if K == 1:
res = S
for i in range(len(S)):
res = min(res, S[i:] + S[:i])
else:
res = "".join(sorted(list(S)))
return res

参考资料:

https://leetcode.com/problems/orderly-queue/discuss/165878/C++JavaPython-Sort-String-or-Rotate-String

日期

2018 年 10 月 4 日 —— 一个很不容易察觉的小错误,需要总结一下坑了!

【LeetCode】899. Orderly Queue 解题报告(Python)的更多相关文章

  1. LeetCode 899. Orderly Queue

    899. Orderly Queue(有序队列) 题目: 给出了一个由小写字母组成的字符串 S.然后,我们可以进行任意次数的移动. 在每次移动中,我们选择前 K 个字母中的一个(从左侧开始),将其从原 ...

  2. [LeetCode] 899. Orderly Queue 有序队列

    A string S of lowercase letters is given.  Then, we may make any number of moves. In each move, we c ...

  3. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  4. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  5. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  6. 【LeetCode】Permutations II 解题报告

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

  7. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  8. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. gg=G

    1.代码格式化对齐 2.直接按下ESE模式下就可以来执行了

  2. 作为Java技术面试官,我如何深挖候选人的技能

    作为Java资深技术面试官,首先我感觉有必要讲解"面试官深挖问题"的动机,在了解动机的前提下,大家才能更好地准备面试.面试官为什么要在一个点上深挖?两大目的.   1 首先是通过深 ...

  3. 以VuePress的v1.x为基础开发-用户手册

    首先配置.vuepress中的config.js module.exports = { title:"用户手册", description: '用户手册', evergreen: ...

  4. 1005.K次取反后最大化的数组和

    1005.K次取反后最大化的数组和 目录 1005.K次取反后最大化的数组和 题目 题解 排序+维护最小值min 题目 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个索引 i 并将 ...

  5. Angular中怎样创建service服务来实现组件之间调用公共方法

    Angular组件之间不能互相调用方法,但是可以通过创建服务来实现公共方法的调用. 实现 创建服务命令 ng g service 服务路径/服务名 比如这里在app/services目录下创建stor ...

  6. [Windows编程]模块遍历

    模块遍历 整体思路 1.创建进程快照 2.遍历首次模块 3.继续下次遍历 4.模块信息结构体 相关API的调用 创建进程快照API HANDLE WINAPI CreateToolhelp32Snap ...

  7. Win7部署Yapi

    1.安装node 下载地址:https://nodejs.org/zh-cn/download/ (win7要下载v12.16之前的版本) 安装目录在D:\nodejs,配置地址(文件目录不能有特殊符 ...

  8. vue 第三方图标库

    "font-awesome": "^4.7.0", "dependencies": { "axios": "^ ...

  9. ActiveRecord教程

    (一.ActiveRecord基础) ActiveRecord是Rails提供的一个对象关系映射(ORM)层,从这篇开始,我们来了解Active Record的一些基础内容,连接数据库,映射表,访问数 ...

  10. Actuator监控器

    一.简介 Actuator(激励者;执行器)是Spring Boot提供的一个可挺拔模块,用于对工程进行监控.其通过不同的监控终端实现不同的监控功能.其功能与Dubbo的监控中心类似,不同的是,Dub ...