3499. Maximize Active Section with Trade I LeetCode Solution

In this guide, you will get 3499. Maximize Active Section with Trade I LeetCode Solution with the best time and space complexity. The solution to Maximize Active Section with Trade I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximize Active Section with Trade I solution in C++
  4. Maximize Active Section with Trade I solution in Java
  5. Maximize Active Section with Trade I solution in Python
  6. Additional Resources
3499. Maximize Active Section with Trade I LeetCode Solution image

Problem Statement of Maximize Active Section with Trade I

You are given a binary string s of length n, where:

‘1’ represents an active section.
‘0’ represents an inactive section.

You can perform at most one trade to maximize the number of active sections in s. In a trade, you:

Convert a contiguous block of ‘1’s that is surrounded by ‘0’s to all ‘0’s.
Afterward, convert a contiguous block of ‘0’s that is surrounded by ‘1’s to all ‘1’s.

Return the maximum number of active sections in s after making the optimal trade.
Note: Treat s as if it is augmented with a ‘1’ at both ends, forming t = ‘1’ + s + ‘1’. The augmented ‘1’s do not contribute to the final count.

Example 1:

Input: s = “01”
Output: 1
Explanation:
Because there is no block of ‘1’s surrounded by ‘0’s, no valid trade is possible. The maximum number of active sections is 1.

Example 2:

Input: s = “0100”
Output: 4
Explanation:

String “0100” → Augmented to “101001”.
Choose “0100”, convert “101001” → “100001” → “111111”.
The final string without augmentation is “1111”. The maximum number of active sections is 4.

Example 3:

Input: s = “1000100”
Output: 7
Explanation:

String “1000100” → Augmented to “110001001”.
Choose “000100”, convert “110001001” → “110000001” → “111111111”.
The final string without augmentation is “1111111”. The maximum number of active sections is 7.

Example 4:

Input: s = “01010”
Output: 4
Explanation:

String “01010” → Augmented to “1010101”.
Choose “010”, convert “1010101” → “1000101” → “1111101”.
The final string without augmentation is “11110”. The maximum number of active sections is 4.

Constraints:

1 <= n == s.length <= 105
s[i] is either '0' or '1'

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

3499. Maximize Active Section with Trade I LeetCode Solution in C++

#include <ranges>

class Solution {
 public:
  int maxActiveSectionsAfterTrade(string s) {
    vector<int> zeroGroupLengths;
    int maxZeroMerge = 0;

    for (int i = 0; i < s.length(); ++i)
      if (s[i] == '0') {
        if (i > 0 && s[i - 1] == '0')
          ++zeroGroupLengths.back();
        else
          zeroGroupLengths.push_back(1);
      }

    for (const auto& [a, b] : zeroGroupLengths | views::pairwise)
      maxZeroMerge = max(maxZeroMerge, a + b);

    return ranges::count(s, '1') + maxZeroMerge;
  }
};
/* code provided by PROGIEZ */

3499. Maximize Active Section with Trade I LeetCode Solution in Java

class Solution {
  public int maxActiveSectionsAfterTrade(String s) {
    List<Integer> zeroGroupLengths = new ArrayList<>();
    int maxZeroMerge = 0;

    for (int i = 0; i < s.length(); ++i) {
      if (s.charAt(i) == '0') {
        if (i > 0 && s.charAt(i - 1) == '0')
          zeroGroupLengths.set(zeroGroupLengths.size() - 1,
                               zeroGroupLengths.get(zeroGroupLengths.size() - 1) + 1);
        else
          zeroGroupLengths.add(1);
      }
    }

    for (int i = 0; i < zeroGroupLengths.size() - 1; ++i)
      maxZeroMerge = Math.max(maxZeroMerge, zeroGroupLengths.get(i) + zeroGroupLengths.get(i + 1));

    return (int) s.chars().filter(c -> c == '1').count() + maxZeroMerge;
  }
}
// code provided by PROGIEZ

3499. Maximize Active Section with Trade I LeetCode Solution in Python

class Solution:
  def maxActiveSectionsAfterTrade(self, s: str) -> int:
    zeroGroups = [len(list(g)) for c, g in itertools.groupby(s) if c == '0']
    return s.count('1') + max(map(sum, pairwise(zeroGroups)), default=0)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.