当前位置:网站首页>【LeetCode】761. Special binary sequence
【LeetCode】761. Special binary sequence
2022-08-08 14:02:00 【pass night】
题目
特殊的二进制序列是具有以下两个性质的二进制序列:
- 0 的数量与 1 的数量相等.
- 二进制序列的每一个前缀码中 1 的数量要大于等于 0 的数量.
给定一个特殊的二进制序列 S,以字符串形式表示.定义一个操作 为首先选择 S 的两个连续且非空的特殊的子串,然后将它们交换.(两个子串为连续的当且仅当第一个子串的最后一个字符恰好为第二个子串的第一个字符的前一个字符.)
在任意次数的操作之后,交换后的字符串按照字典序排列的最大的结果是什么?
示例 1:
输入: S = "11011000"
输出: "11100100"
解释:
将子串 "10" (在S[1]出现) 和 "1100" (在S[3]出现)进行交换.
这是在进行若干次操作后按字典序排列最大的结果.
说明:
S的长度不超过50.S保证为一个满足上述定义的特殊 的二进制序列.
思路
- Because it can only operate on special substrings that are continuous and non-empty,So declare a variable to judge whether the current interval is a continuous substring during the traversal process
- 因为0,1数量相等,and prefix1的数量大于0,So special strings are given by 1开头,0结尾
- Continually recurse the special strings seen,Finds the largest lexicographic value of its substring each time,After splicing, it is still the largest lexicographical order
- Although only two adjacent characters can be swapped,But according to the principle of bubble sort,Ascending sequences are still available
- The ascending sequence described above is evaluated in bits
代码
class Solution:
def makeLargestSpecial(self, s: str) -> str:
if len(s) <= 2:
return s
count = left = 0
subs = list()
for i, c in enumerate(s):
if c == "1": count +=1
else:
count -= 1
if count == 0:
subs.append("1" + self.makeLargestSpecial(s[left+1:i]) + "0")
left = i+1
subs.sort(reverse=True)
return "".join(subs)
复杂度
- 时间复杂度: O ( n 2 ) O(n^2) O(n2)
- 空间复杂度: O ( n ) O(n) O(n)
边栏推荐
- 【Redis】redis安装与客户端redis-cli的使用(批量操作)
- flutter 身兼数职的getx —— 简介
- 接口测试,
- 【小码匠自习室】叛逆的小孩,打死也不改
- Time to update your tech arsenal in 2020: Asgi vs Wsgi (FastAPI vs Flask)
- Tensorflow and Keras for machine learning, deep learning
- LeetCode简单题之统计星号
- R语言ggplot2可视化:使用ggpubr包的ggline函数可视化折线图(点线图、line plot)、设置add参数为mean可视化不同水平均值的折线图
- 年初离职,学习半年源码,终于拿到了蚂蚁Offer,分享面试过程
- 全网最全的PADS 9.5安装教程与资源包
猜你喜欢

KD-SCFNet: More Accurate and Efficient Salient Object Detection Through Knowledge Distillation (ECCV2022)

HackTheBox | Previse

Thesis understanding: "Self-adaptive loss balanced Physics-informed neural networks"

数据解析(XPath、BeautifulSoup、正则表达式、pyquery)

你是什么时候对深度学习失去信心的?

Review: What is the pre-approval of autumn recruitment?What is an ordinary autumn move?It's all recruitment, why do you need to set these two recruitment time periods?

客户案例 | 提高银行信用卡客户贡献率

【黑马早报】巴菲特罕见巨亏近3000亿;周鸿祎回应360不能卸载;三亚倡议酒店不变相提高房价;首个国产抗新冠口服药定价不超300元...

代码随想录笔记_动态规划_322零钱兑换

张一鸣挺进生育大业
随机推荐
【电路基础2】电容
【Redis】redis安装与客户端redis-cli的使用(批量操作)
【黑马早报】巴菲特罕见巨亏近3000亿;周鸿祎回应360不能卸载;三亚倡议酒店不变相提高房价;首个国产抗新冠口服药定价不超300元...
全网最全的PADS 9.5安装教程与资源包
R语言ggplot2可视化:基于aes函数中的fill参数和shape参数自定义绘制分组折线图并添加数据点(散点)、设置可视化图像的主题为theme_gray
【小码匠自习室】CSP-J/S复试高分秘诀经验分享
2022年8月7日 暑假第四周总结
跟我一起了解云耀云服务器HECS【华为云至简致远】
兔起鹘落全端涵盖,Go lang1.18入门精炼教程,由白丁入鸿儒,全平台(Sublime 4)Go lang开发环境搭建EP00
【Rust—LeetCode题解】1408.数组中的字符串匹配
年初离职,学习半年源码,终于拿到了蚂蚁Offer,分享面试过程
彻底了解什么是POE交换机!!!
连锁小酒馆第一股,海伦司能否梦圆大排档?
poj2096 Collecting Bugs
R语言ggplot2可视化:使用ggpubr包的ggtexttable函数可视化表格数据(直接绘制表格图或者在图像中添加表格数据)、使用tab_add_hline函数为表头添加横线并自定义线条宽度
树上距离为1子集修改
flutter 身兼数职的getx —— 简介
清华|GLM-130B:一个开放的双语预训练模型
你是什么时候对深度学习失去信心的?
HackTheBox | Previse