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. 解决 “version `GLIBCXX_3.4.21' not found ”问题

    https://blog.csdn.net/Heldrecom/article/details/85040411

  2. LuoguP5290 [十二省联考2019]春节十二响 | 启发式合并

    还有33天就要高考了,我在干啥-- 题目概述 一棵有根树,每个节点有权值. 要求把所有节点分成组,具有祖先-后代关系的两个节点不能被分到同一组. 每一组的代价是所包含的节点的最大权值,最小化所有组的代 ...

  3. ansible碎碎念

    1. Using a SSH password instead of a key is not possible because Host Key checking is enabled and ss ...

  4. 如何查询正在运行的SQL Server agent job

    运行"msdb"系统数据库下的存储过程"dbo.sp_help_job",可以得知现在SQL Server中有多少个正在运行的agent job: USE [m ...

  5. Out,ref,params修饰符,可选参数,命名参数

    out输出,在调用函数中声明,在被调用函数中赋值: ref在调用函数中赋值,后调用: params修饰符,static double CalculateAverage(params[] values) ...

  6. 引用kernel32.dll中的API来进行串口通讯

    串口通讯可以引出kernel32.dll中的API来操作,相关源码如下:using System;using System.Runtime.InteropServices; namespace Tel ...

  7. 关于前端ajax请求url为何添加一个随机数

    一.起因 我在做爬虫的时候发现很多网站上都在url上加一个随机数或者时间戳一开始我以为是啥加密后面发现其实他在后台解析的时候也不需要不排除有些网站他反爬就需要一个时间戳,那他加个随机数是做啥子 二.查 ...

  8. Winform中设置多条Y轴时新增的Y轴刻度不显示问题解决

    场景 Winform中实现ZedGraph的多条Y轴(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1001322 ...

  9. ubuntu 18.04 修改Apache默认目录

    ubuntu 18.04 修改Apache默认目录 安装是直接运行 sudu apt install apache2 安装之后要修改目录 vi /etc/apache2/sites-available ...

  10. js中对象字面量

    一.对象字面量语法 var person={ name:'小王', age:18, _pri:233 } 成员名称的单引号不是必须的 最后一个成员结尾不要用逗号,不然在某些浏览器中会抛出错误 成员名相 ...