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个字母中的任意一个移动到末尾,让我们返回可以变换成的字母顺序最小的字符串。刚开始看到的时候,博主感觉就是一个 BFS 遍历,对于每一个状态,都生成K个新的状态,然后将没有遍历过的加入 queue 中去遍历,然后维护一个全局最小的 res 即可,写完之后拿到 OJ 中去测试了,结果跪了,Time Limit Exceeded!心想着,还能怎么优化呢?一逛论坛后发现,这道题还是真是有 trick 的,如果不仔细想,感觉不太容易想到。正确的思路其实是跟K值有关的,若 K=1,其实只有K中不同的情况,我们可以都生成,然后比较出其中最小的那个返回即可。关键是 K>1 的时候,比较 tricky,其实是可以转换成有序的,即相当于直接对S串进行排序即可。我们就拿 S="53214", K=2 来举例吧,转换过程如下所示:

5 3 2 1 4
3 2 1 4 5
3 1 4 5 2
1 4 5 2 3
1 5 2 3 4
1 2 3 4 5

虽然不知道如何严格的证明当 K>1 时,一定能转成有序的排序,但是博主试了几个例子,都是可以的,论坛上说是一种类似于冒泡排序 Bubble Sort 的过程。若有哪位看官大神们知道如何证明,请务必留言告诉博主哈,参见代码如下:

class Solution {
public:
string orderlyQueue(string S, int K) {
if (K > 1) {
sort(S.begin(), S.end());
return S;
}
string res = S;
for (int i = 0; i < S.size(); ++i) {
res = min(res, S.substr(i) + S.substr(0, i));
}
return res;
}
};

讨论:微信公众号粉丝 YF 童鞋提供了一种不严格的证明过程。只要证明 k=2 能将任意字符串转为有序的,那么对于任意 k>1 的情况都是成立的。对于任意顺序,我们都可以现将最小的数字移动到末尾,形成 xxxxx1 这种类型的,然后一定有办法将第二小的数字移动到末尾,变成 xxxx12,以此类推类推,可以将所有数字按顺序移动到末尾,形成类似冒泡排序的操作,拿 871524 来举例:

  • 将1移动到末尾
8 7 1 5 2 4
7 1 5 2 4 8
1 5 2 4 8 7
5 2 4 8 7 1
  • 将2移动到末尾
5 2 4 8 7 1
5 4 8 7 1 2
  • 将4移动到末尾
5 4 8 7 1 2
5 8 7 1 2 4
  • 将5移动到末尾
5 8 7 1 2 4
8 7 1 2 4 5
  • 将7移动到末尾
8 7 1 2 4 5
8 1 2 4 5 7
  • 将8移动到末尾
8 1 2 4 5 7
1 2 4 5 7 8

Github 同步地址:

https://github.com/grandyang/leetcode/issues/899

参考资料:

https://leetcode.com/problems/orderly-queue/

https://leetcode.com/problems/orderly-queue/discuss/165862/Kgreater1-is-bubblesort

https://leetcode.com/problems/orderly-queue/discuss/165878/C%2B%2BJavaPython-Sort-String-or-Rotate-String

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 899. Orderly Queue 有序队列的更多相关文章

  1. LeetCode 899. Orderly Queue

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

  2. 【LeetCode】899. Orderly Queue 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/orderly- ...

  3. 899. Orderly Queue

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

  4. python-Day3-set 集合-counter计数器-默认字典(defaultdict) -可命名元组(namedtuple)-有序字典(orderedDict)-双向队列(deque)--Queue单项队列--深浅拷贝---函数参数

    上节内容回顾:C语言为什么比起他语言块,因为C 会把代码变异成机器码Pyhton 的 .pyc文件是什么python 把.py文件编译成的.pyc文件是Python的字节码, 字符串本质是 字符数组, ...

  5. LeetCode 622:设计循环队列 Design Circular Queue

    LeetCode 622:设计循环队列 Design Circular Queue 首先来看看队列这种数据结构: 队列:先入先出的数据结构 在 FIFO 数据结构中,将首先处理添加到队列中的第一个元素 ...

  6. [LeetCode] 面试题59 - II. 队列的最大值

    题目: 分析: 本题要求三个方法的时间复杂度都是O(1),对于push_back和pop_front都是好实现的 但是对于max_value,正常情况下要进行遍历才能获得最大值,那么如何才能在O(1) ...

  7. Queue 先进先出队列的操作

    1.Queue定义 System.Collections.Queue类表示对象的先进先出集合,存储在 Queue(队列) 中的对象在一端插入,从另一端移除. 2.优点 1.能对集合进行顺序处理(先进先 ...

  8. C++数据结构之Queue(队列)

    Queue,队列,和我们日常生活中的队列是同样的规则,"先进先出",从尾入,从首出. Queue,主要有三种基本操作,append(添加元素至队尾):serve(队首元素出列):r ...

  9. 双有序队列算法——处理哈夫曼K叉树的高效算法

    算法介绍: 哈夫曼树的思路及实现众所周知,大部分是用堆来维护和实现,这种思路比较清晰,在K比较小的时候处理较快(具体例子接下来再说),而且编程复杂度不是很高,利于应用.但是,其所用的数据结构是树,是在 ...

随机推荐

  1. python-10-列表、元组嵌套

    前言 元组.列表前面章节有讲解实例,本节内容是列表.元组的多嵌套. 一.列表嵌套 1.列表嵌套操作1 # 列表的嵌套 li = ['xiaolong', '小林', ['小龙', 'xiaol'], ...

  2. pixijs shader 扫光加强版

    pixijs shader 扫光加强版 const app = new PIXI.Application({ transparent: true }); document.body.appendChi ...

  3. jquery ajax怎么使用jsonp跨域访问

    在项目中使用接口的比较多,在客户端跨域访问,jquery中只能使用jquery ajax的jsonp方法. 值得注意的是,jQuery.ajax()只支持get方式的跨域,post的方式是不支持的.& ...

  4. fatal error compiling: tools.jar not found

    在Eclipse中使用Maven提供的Install(打包)命令插件的时候报错[Fatal error compiling: tools.jar not found]. 报错的原因 报错的原因从错误信 ...

  5. Spring源码系列 — Envoriment组件

    何为Envoriment Envoriment是集成在Spring上下文容器中的核心组件,在Spring源码中由Envoriment接口抽象. 在Environment中,有两大主要概念: Profi ...

  6. Python - 输入和输出 - 第十七天

    Python 输入和输出 在前面几个章节中,我们其实已经接触了 Python 的输入输出的功能.本章节我们将具体介绍 Python 的输入输出. 输出格式美化 Python两种输出值的方式: 表达式语 ...

  7. 工作笔记--Python自动切换host

    修改host代码: #coding:utf-8import os,time pwd = os.path.dirname(__file__) #获取当前文件夹的绝对路径pull_host_cmd = ' ...

  8. 【设计模式】Composite

    目录 前言 安卓View的实现 View Beyond setContentView setContentView做了什么事情? 如何将xml文件变成对象的? 小结 View的绘制流程 三个流程 三个 ...

  9. crontab运行python不生效,但是手动执行正常的问题和解决方案

    crontab运行python不生效,但是手动执行正常的问题和解决方案 linux默认装的是python2.7,安装了其他版本后直接执行没问题,但在crontab里执行不了,需要使用全路径. 使用 w ...

  10. 英语AquilariaCrassna奇楠沉香

    越南奇楠沉香Aquilaria crassna是瑞香科沉香属植物. 奇楠香被喻为沉香中的钻石,其与身俱来的香气,淡雅宜人,汇集天地阴阳五行之气,而成为唯一能通三界之香品.长久以来,它被视为一种珍贵罕有 ...