LeetCode 题解工作台

修改后的最大二进制字符串

给你一个二进制字符串 binary ,它仅有 0 或者 1 组成。你可以使用下面的操作任意次对它进行修改: 操作 1 :如果二进制串包含子字符串 "00" ,你可以用 "10" 将其替换。 比方说, " 00 010" -> " 10 010" 操作 2 :如果二进制串包含子字符串 "10" ,你可…

category

2

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

中等 · 贪心·invariant

bolt

答案摘要

我们观察发现,操作 可以把所有的 都移动到字符串的末尾,而操作 可以把所有的 `0000..000` 串变为 `111..110`。 因此,要想得到最大的二进制串,我们应该把所有不在开头的 移动到字符串末尾,使得字符串变为 `111..11...000..00..11` 的形式,然后借助操作 把中间的 `000..00` 变为 `111..10`。这样我们最终可以得到一个最多包含一个 …

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 贪心·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个二进制字符串 binary ,它仅有 0 或者 1 组成。你可以使用下面的操作任意次对它进行修改:

  • 操作 1 :如果二进制串包含子字符串 "00" ,你可以用 "10" 将其替换。
    • 比方说, "00010" -> "10010"
  • 操作 2 :如果二进制串包含子字符串 "10" ,你可以用 "01" 将其替换。
    • 比方说, "00010" -> "00001"

请你返回执行上述操作任意次以后能得到的 最大二进制字符串 。如果二进制字符串 x 对应的十进制数字大于二进制字符串 y 对应的十进制数字,那么我们称二进制字符串 x 大于二进制字符串 y 

 

示例 1:

输入:binary = "000110"
输出:"111011"
解释:一个可行的转换为:
"000110" -> "000101" 
"000101" -> "100101" 
"100101" -> "110101" 
"110101" -> "110011" 
"110011" -> "111011"

示例 2:

输入:binary = "01"
输出:"01"
解释:"01" 没办法进行任何转换。

 

提示:

  • 1 <= binary.length <= 105
  • binary 仅包含 '0' 和 '1'
lightbulb

解题思路

方法一:脑筋急转弯

我们观察发现,操作 22 可以把所有的 11 都移动到字符串的末尾,而操作 11 可以把所有的 0000..000 串变为 111..110

因此,要想得到最大的二进制串,我们应该把所有不在开头的 11 移动到字符串末尾,使得字符串变为 111..11...000..00..11 的形式,然后借助操作 11 把中间的 000..00 变为 111..10。这样我们最终可以得到一个最多包含一个 00 的二进制字符串,这个字符串就是我们要求的最大二进制串。

在代码实现上,我们首先判断字符串是否包含 00,如果不包含,直接返回原字符串。否则,我们找到第一个 00 的位置 kk,加上该位置之后的 00 的个数,得到的就是修改后的字符串的 00 所在的位置,其余位置都是 11

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n)。其中 nn 是字符串的长度。

1
2
3
4
5
6
7
8
class Solution:
    def maximumBinaryString(self, binary: str) -> str:
        k = binary.find('0')
        if k == -1:
            return binary
        k += binary[k + 1 :].count('0')
        return '1' * k + '0' + '1' * (len(binary) - k - 1)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Evaluate understanding of greedy algorithms in string manipulation.

  • question_mark

    Look for the candidate's ability to handle edge cases, such as no valid operations or already maximized strings.

  • question_mark

    Assess their ability to optimize time complexity and avoid unnecessary operations.

warning

常见陷阱

外企场景
  • error

    Failing to recognize that only one zero can remain in the string after transformations.

  • error

    Not validating the string structure after each operation and missing potential '01' pairs to change.

  • error

    Overcomplicating the solution by applying unnecessary operations or using suboptimal approaches.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if you were allowed to apply the operation multiple times in a row for the same '01' pair?

  • arrow_right_alt

    How would the solution change if you had to ensure the resulting string contains at least one '1'?

  • arrow_right_alt

    Can this approach be adapted to other string manipulation problems with different operations?

help

常见问题

外企场景

修改后的最大二进制字符串题解:贪心·invariant | LeetCode #1702 中等