当前位置:网站首页>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)的空间
边栏推荐
- [redis] cache penetration and cache avalanche and cache breakdown solutions
- 303. Range Sum Query - Immutable
- 204. Count Primes
- 数字资产的价值激发:NFT 质押
- 数据中台“集存通用治”功能场景说明
- Promise的 简单使用
- 测试测试测试
- 论文解读(JKnet)《Representation Learning on Graphs with Jumping Knowledge Networks》
- sibling component communication context
- protobuf 反射使用总结
猜你喜欢
为何微博又双叒叕崩溃了?
华为、联想、北汽等入选工信部“企业数字化转型和安全能力提升”首批实训基地
Web3 安全风险令人生畏?应该如何应对?
The strongest distributed lock tool: Redisson
《社会企业开展应聘文职人员培训规范》团体标准在新华书店上架
如何设计大电流九线导电滑环
Cookie和Session的关系
Components of communication - the drop-down menu
[Unity Getting Started Plan] Basic Concepts (7) - Input Manager & Input Class
基于DMS的数仓智能运维服务,知多少?
随机推荐
测试测试测试
C专家编程 第1章 C:穿越时空的迷雾 1.11 轻松一下---由编译器定义的Pragmas效果
MySQL查询语法
Excuse me this hologres dimension table is cached?How to Finished
兄弟组件通信context
uniapp 去掉默认导航栏
uniapp 切换 history 路由模
[redis] cache penetration and cache avalanche and cache breakdown solutions
EA 改口,称单人游戏是产品组合中“非常重要的一部分”
After using Stream for many years, does collect still have these "saucy operations"?
node连接mongoose数据库流程
leetcode:189. 轮转数组
如何直击固定资产管理的难题?
MobileVIT实战:使用MobileVIT实现图像分类
高效的组织信息共享知识库是一种宝贵的资源
C专家编程 第1章 C:穿越时空的迷雾 1.10 “安静的改变”究竟有多少安静
#夏日挑战赛#【FFH】OpenHarmony设备开发基础(四)启动流程
How ArkUI adapter somehow the screen
102. 最佳牛围栏
Cookie和Session的关系