LeetCode 题解工作台

操作后最大活跃区段数 I

给你一个长度为 n 的二进制字符串 s ,其中: '1' 表示一个 活跃 区段。 '0' 表示一个 非活跃 区段。 你可以执行 最多一次操作 来最大化 s 中的活跃区段数量。在一次操作中,你可以: 将一个被 '0' 包围的连续 '1' 区块转换为全 '0' 。 然后,将一个被 '1' 包围的连续 '…

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · string·结合·enumeration

bolt

答案摘要

题目实际上等价于求字符串 中,字符 `'1'` 的数量,再加上相邻两个连续的字符 `'0'` 串中 ``'0'` 的最大数量。 因此,我们可以使用双指针来遍历字符串 ,用一个变量 来记录相邻两个连续的字符 `'0'` 串中 `'0'` 的最大数量。我们还需要一个变量 来记录上一个连续的字符 `'0'` 串的数量。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 string·结合·enumeration 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个长度为 n 的二进制字符串 s,其中:

  • '1' 表示一个 活跃 区段。
  • '0' 表示一个 非活跃 区段。

你可以执行 最多一次操作 来最大化 s 中的活跃区段数量。在一次操作中,你可以:

  • 将一个被 '0' 包围的连续 '1' 区块转换为全 '0'
  • 然后,将一个被 '1' 包围的连续 '0' 区块转换为全 '1'

返回在执行最优操作后,s 中的 最大 活跃区段数。

注意:处理时需要在 s 的两侧加上 '1' ,即 t = '1' + s + '1'。这些加上的 '1' 不会影响最终的计数。

 

示例 1:

输入: s = "01"

输出: 1

解释:

因为没有被 '0' 包围的 '1' 区块,因此无法进行有效操作。最大活跃区段数为 1。

示例 2:

输入: s = "0100"

输出: 4

解释:

  • 字符串 "0100" → 两端加上 '1' 后得到 "101001" 。
  • 选择 "0100""101001""100001""111111" 。
  • 最终的字符串去掉两端的 '1' 后为 "1111" 。最大活跃区段数为 4。

示例 3:

输入: s = "1000100"

输出: 7

解释:

  • 字符串 "1000100" → 两端加上 '1' 后得到 "110001001" 。
  • 选择 "000100""110001001""110000001""111111111"
  • 最终的字符串去掉两端的 '1' 后为 "1111111"。最大活跃区段数为 7。

示例 4:

输入: s = "01010"

输出: 4

解释:

  • 字符串 "01010" → 两端加上 '1' 后得到 "1010101"
  • 选择 "010""1010101""1000101""1111101"
  • 最终的字符串去掉两端的 '1' 后为 "11110"。最大活跃区段数为 4。

 

提示:

  • 1 <= n == s.length <= 105
  • s[i] 仅包含 '0''1'
lightbulb

解题思路

方法一:贪心 + 双指针

题目实际上等价于求字符串 s\textit{s} 中,字符 '1' 的数量,再加上相邻两个连续的字符 '0' 串中 ``'0'` 的最大数量。

因此,我们可以使用双指针来遍历字符串 s\textit{s},用一个变量 mx\textit{mx} 来记录相邻两个连续的字符 '0' 串中 '0' 的最大数量。我们还需要一个变量 pre\textit{pre} 来记录上一个连续的字符 '0' 串的数量。

每一次,我们统计当前连续相同字符的数量 cnt\textit{cnt},如果当前字符为 '1',则将 cnt\textit{cnt} 加入到答案中;如果当前字符为 '0',则将 mx\textit{mx} 更新为 mx=max(mx,pre+cnt)\textit{mx} = \max(\textit{mx}, \textit{pre} + \textit{cnt}),并将 pre\textit{pre} 更新为 cnt\textit{cnt}。最后,我们将答案加上 mx\textit{mx} 即可。

时间复杂度 O(n)O(n),其中 nn 为字符串 s\textit{s} 的长度。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def maxActiveSectionsAfterTrade(self, s: str) -> int:
        n = len(s)
        ans = i = 0
        pre, mx = -inf, 0
        while i < n:
            j = i + 1
            while j < n and s[j] == s[i]:
                j += 1
            cur = j - i
            if s[i] == "1":
                ans += cur
            else:
                mx = max(mx, pre + cur)
                pre = cur
            i = j
        ans += mx
        return ans
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    The candidate identifies key segments of '0's and '1's and their potential for creating additional sections.

  • question_mark

    They analyze the impact of different trade scenarios on the string's structure.

  • question_mark

    They demonstrate an efficient approach to evaluating and maximizing the number of active sections.

warning

常见陷阱

外企场景
  • error

    Not correctly identifying segments of '0's and '1's for potential trades.

  • error

    Assuming that any '0' block can be flipped, when only '0' blocks surrounded by '1's are relevant.

  • error

    Overcomplicating the problem by attempting to perform unnecessary trades that don’t increase the number of sections.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Consider cases where the string consists entirely of '0's or '1's.

  • arrow_right_alt

    Handle strings with mixed lengths of '0's and '1's, ensuring the trade maximizes sections.

  • arrow_right_alt

    Analyze larger inputs where performance optimizations become more critical.

help

常见问题

外企场景

操作后最大活跃区段数 I题解:string·结合·enumeration | LeetCode #3499 中等