当前位置:网站首页>leetcode-每日一题899. 有序队列(思维题)
leetcode-每日一题899. 有序队列(思维题)
2022-08-03 16:53:00 【lin钟一】

题目链接:https://leetcode.cn/problems/orderly-queue/
思路
方式一、思维
计算字典序最小的字符串,我们需要讨论k = 1 和 k > 1时两种情况
当k = 1时,我们每次取 i 个首字符并将其移动到字符串末尾,对比找最小的字典序字符串即可
当k > 1时,一定可以经过移动将字符串s变成字符串按照升序排序
代码示例
func orderlyQueue(s string, k int) string {
if k == 1{
ans := s
for i := 1; i < len(s); i++{
s = s[1:] + s[:1]
if s < ans{
ans = s
}
}
return ans
}
ans := []byte(s)
sort.Slice(ans, func(i, j int) bool {
return ans[i] < ans[j]})
return string(ans)
}
复杂度分析
- 时间复杂度:O(n2),其中n时字符串长度
- 当k = 1时,需要遍历n个字符串排列,每个字符串需要O(n)来判断是否为字典序最小,需要O(n2)的时间
- 当k > 1时,对字符串进行排序,需要O(n logn)的时间
- 空间复杂度:O(n),其中n时字符串长度,需要额外申请O(n)的空间
边栏推荐
- 华为、联想、北汽等入选工信部“企业数字化转型和安全能力提升”首批实训基地
- Understand the recommendation system in one article: Outline 02: The link of the recommendation system, from recalling rough sorting, to fine sorting, to rearranging, and finally showing the recommend
- 2022爱分析· 银行数字化厂商全景报告
- 大佬们。使用flink-cdc-sqlserver 2.2.0 版本读取sqlserver2008R
- C专家编程 第1章 C:穿越时空的迷雾 1.10 “安静的改变”究竟有多少安静
- 兄弟组件通信context
- SQL中对 datetime 类型操作
- 数据中台“集存通用治”功能场景说明
- 【无标题】
- 国内首发可视化智能调优平台,小龙带你玩转KeenTune UI
猜你喜欢

最强分布式锁工具:Redisson

Understand the recommendation system in one article: Outline 02: The link of the recommendation system, from recalling rough sorting, to fine sorting, to rearranging, and finally showing the recommend

【数据库数据恢复】SqlServer数据库无法读取的数据恢复案例
[redis] cache penetration and cache avalanche and cache breakdown solutions

J9数字虚拟论:元宇宙的潜力:一股推动社会进步的力量

fastposter v2.9.0 程序员必备海报生成器

如何选择合适的导电滑环型号

error:Illegal instruction (core dumped),离线下载安装这个other版本numpy

产品-Axure9英文版,轮播图效果

Adobe是什么?
随机推荐
自动化部署+整合SSM项目
B站回应HR称核心用户是Loser;微博回应宕机原因;Go 1.19 正式发布|极客头条
MySQL查询语法
被误解的 MVC 和被神化的 MVVM(一)
如何设计大电流九线导电滑环
【LeetCode】899. 有序队列
Auto Scaling 弹性伸缩(运维释放人力)
JS 字符串转 GBK 编码超精简实现
工程仪器设备在线监测管理系统常见问题和注意事项
C专家编程 第2章 这不是Bug,而是语言特性 2.4 少做之过
EasyExcel implements dynamic column parsing and table storage
php之相似文章标题similar_text()函数使用
Web3的开源为何会如此受到人们喜爱?
How to write SQL statements in DataWorks monitoring data reaches a certain value to indicate the change of
C语言01、数据类型、变量常量、字符串、转义字符、注释
error:Illegal instruction (core dumped),离线下载安装这个other版本numpy
EA 改口,称单人游戏是产品组合中“非常重要的一部分”
node连接mongoose数据库流程
C专家编程 第3章 分析C语言的声明 3.8 理解所有分析过程的代码段
设置海思芯片MMZ内存、OS内存详解