当前位置:网站首页>899. 有序队列 : 最小表示法模板题
899. 有序队列 : 最小表示法模板题
2022-08-03 11:31:00 【宫水三叶的刷题日记】
题目描述
这是 LeetCode 上的 899. 有序队列 ,难度为 困难。
Tag : 「构造」、「最小表示法」
给定一个字符串 s 和一个整数 k 。你可以从 s 的前 k 个字母中选择一个,并把它加到字符串的末尾。
返回 在应用上述步骤的任意数量的移动后,字典上最小的字符串 。
示例 1:
输入:s = "cba", k = 1
输出:"acb"
解释:
在第一步中,我们将第一个字符(“c”)移动到最后,获得字符串 “bac”。
在第二步中,我们将第一个字符(“b”)移动到最后,获得最终结果 “acb”。
示例 2:
输入:s = "baaca", k = 3
输出:"aaabc"
解释:
在第一步中,我们将第一个字符(“b”)移动到最后,获得字符串 “aacab”。
在第二步中,我们将第三个字符(“c”)移动到最后,获得最终结果 “aaabc”。
提示:
s只由小写字母组成。
最小表示法
当 时,我们能够构造出任意的字符串方案,因此当 时,我们可以直接通过对字符串排序来得到答案,复杂度为 。
当 时,我们共有 种候选方案(将字符串 s 看作一个首尾相接的循环字符串,共有 个起点可枚举),枚举过程中需要与当前最优的方案进行比较,比较复杂度为 ,因此整体复杂度为 。
上述的做法已经可以通过本题,可以看出瓶颈在于对 的处理。
而实际上,对于给定字符串 s,求其循环同构的所有方案中字典序最小的方案,可以使用「最小表示法」来做,复杂度为 。
最小表示法将「方案比较」与「构造更优方案」进行结合:假设我们当前有两字符串 a 和 b 需要进行比较,其均为原串 s 的循环同构具体方案。假设 a 和 b 分别对应了原串下标为 i 和 j 的具体方案,且假设两字符串前 个字符均相同。当两字符串第一个不同的字符大小关系为 时,可以发现以下标范围 作为起点的新方案必然不可能比字符串 b 更优,因此我们可以直接从 位置构造新的更优方案,并与 b 再次比较。而 的分析同理。
Java 代码:
class Solution {
public String orderlyQueue(String s, int _k) {
char[] cs = s.toCharArray();
if (_k == 1) {
int i = 0, j = 1, k = 0, n = cs.length;
while (i < n && j < n && k < n) {
char a = cs[(i + k) % n], b = cs[(j + k) % n];
if (a == b) k++;
else {
if (a > b) i += k + 1;
else j += k + 1;
if (i == j) i++;
k = 0;
}
}
i = Math.min(i, j);
return s.substring(i) + s.substring(0, i);
} else {
Arrays.sort(cs);
return String.valueOf(cs);
}
}
}
TypeScript 代码:
function orderlyQueue(s: string, _k: number): string {
if (_k == 1) {
let i = 0, j = 1, k = 0, n = s.length
while (i < n && j < n && k < n) {
const a = s[(i + k) % n], b = s[(j + k) % n]
if (a == b) k++;
else {
if (a > b) i += k + 1
else j += k + 1
if (i == j) i++
k = 0
}
}
i = Math.min(i, j)
return s.substring(i) + s.substring(0, i)
} else {
return [...s].sort().join('');
}
};
时间复杂度:当 时,复杂度为 ;当 时,复杂度为 空间复杂度:当 时,需要使用额外的排序空间
最后
这是我们「刷穿 LeetCode」系列文章的第 No.899 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地
边栏推荐
猜你喜欢

Analysis of the idea of the complete knapsack problem

CADEditorX ActiveX 14.1.X

数据库一席谈:打造开源的数据生态,支撑产业数字化浪潮

Polymorphism in detail (simple implementation to buy tickets system simulation, covering/weight definition, principle of polymorphism, virtual table)

What is the relationship between The Matrix and 6G?

fast planner中拓扑路径搜索

3分钟实现内网穿透(基于ngrok实现)

性能优化|从ping延时看CPU电源管理

Simple implementation of a high-performance clone of Redis using .NET (1)

Summary of redis basics - data types (strings, lists, sets, hashes, sets)
随机推荐
【网络原理的概念】
机器比人更需要通证
【一起学Rust 基础篇】Rust基础——变量和数据类型
[错题]电路维修
使用.NET简单实现一个Redis的高性能克隆版(一)
For invoice processing DocuWare, cast off the yoke of the paper and data input, automatic processing all the invoice received
深度学习:文本CNN-textcnn
ABAB-740新语法
小身材有大作用——光模块寿命分析(二)
Skills required to be a good architect: How to draw a system architecture that everyone will love?What's the secret?Come and open this article to see it!...
Programmers architecture practice way: software architecture basic concepts and thinking
在线生成接口文档
dataset数据集有哪些_数据集类型
实现2d人物在跳跃的同时左右移动
Cookie和Session使用
技术总监需要会些什么?也太难了!
ERC20通证标准是什么?
asdn涨薪技术之apifox+Jenkins如何玩转接口自动化测试
Why is the new earth blurred, in-depth analysis of white balls, viewing pictures, and downloading problems
矩阵的计算[通俗易懂]