3480. Maximize Subarrays After Removing One Conflicting Pair LeetCode Solution

In this guide, you will get 3480. Maximize Subarrays After Removing One Conflicting Pair LeetCode Solution with the best time and space complexity. The solution to Maximize Subarrays After Removing One Conflicting Pair 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 Subarrays After Removing One Conflicting Pair solution in C++
  4. Maximize Subarrays After Removing One Conflicting Pair solution in Java
  5. Maximize Subarrays After Removing One Conflicting Pair solution in Python
  6. Additional Resources
3480. Maximize Subarrays After Removing One Conflicting Pair LeetCode Solution image

Problem Statement of Maximize Subarrays After Removing One Conflicting Pair

You are given an integer n which represents an array nums containing the numbers from 1 to n in order. Additionally, you are given a 2D array conflictingPairs, where conflictingPairs[i] = [a, b] indicates that a and b form a conflicting pair.
Remove exactly one element from conflictingPairs. Afterward, count the number of non-empty subarrays of nums which do not contain both a and b for any remaining conflicting pair [a, b].
Return the maximum number of subarrays possible after removing exactly one conflicting pair.

Example 1:

Input: n = 4, conflictingPairs = [[2,3],[1,4]]
Output: 9
Explanation:

Remove [2, 3] from conflictingPairs. Now, conflictingPairs = [[1, 4]].
There are 9 subarrays in nums where [1, 4] do not appear together. They are [1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 3] and [2, 3, 4].
The maximum number of subarrays we can achieve after removing one element from conflictingPairs is 9.

See also  122. Best Time to Buy and Sell Stock II LeetCode Solution

Example 2:

Input: n = 5, conflictingPairs = [[1,2],[2,5],[3,5]]
Output: 12
Explanation:

Remove [1, 2] from conflictingPairs. Now, conflictingPairs = [[2, 5], [3, 5]].
There are 12 subarrays in nums where [2, 5] and [3, 5] do not appear together.
The maximum number of subarrays we can achieve after removing one element from conflictingPairs is 12.

Constraints:

2 <= n <= 105
1 <= conflictingPairs.length <= 2 * n
conflictingPairs[i].length == 2
1 <= conflictingPairs[i][j] <= n
conflictingPairs[i][0] != conflictingPairs[i][1]

Complexity Analysis

  • Time Complexity: O(n + \texttt{conflictPairs})
  • Space Complexity: O(n)

3480. Maximize Subarrays After Removing One Conflicting Pair LeetCode Solution in C++

class Solution {
 public:
  long long maxSubarrays(int n, vector<vector<int>>& conflictingPairs) {
    long validSubarrays = 0;
    int maxLeft = 0;
    int secondMaxLeft = 0;
    // gains[i] := the number of additional valid subarrays we can gain if the
    // restriction at index `i` is removed
    vector<long> gains(n + 1);
    // conflicts[r] := left endpoints that conflict with subarrays ending in r
    vector<vector<int>> conflicts(n + 1);

    for (const vector<int>& pair : conflictingPairs) {
      const int a = pair[0];
      const int b = pair[1];
      conflicts[max(a, b)].push_back(min(a, b));
    }

    for (int right = 1; right <= n; ++right) {
      for (const int left : conflicts[right]) {
        if (left > maxLeft) {
          secondMaxLeft = maxLeft;
          maxLeft = left;
        } else if (left > secondMaxLeft) {
          secondMaxLeft = left;
        }
      }
      // Subarrays [maxLeft + 1..right],
      //           [maxLeft + 2..right],
      //           ...
      //           [right..right] are valid.
      validSubarrays += right - maxLeft;
      // If we remove `maxLeft` (the most restrictive conflict), we gain
      // `maxLeft - secondMaxLeft` new subarrays:
      // [secondMaxLeft + 1..right],
      // [secondMaxLeft + 2..right],
      // ...
      // [maxLeft..right].
      gains[maxLeft] += maxLeft - secondMaxLeft;
    }

    return validSubarrays + ranges::max(gains);
  }
};
/* code provided by PROGIEZ */

3480. Maximize Subarrays After Removing One Conflicting Pair LeetCode Solution in Java

class Solution {
  public long maxSubarrays(int n, int[][] conflictingPairs) {
    long validSubarrays = 0;
    int maxLeft = 0;
    int secondMaxLeft = 0;
    // gains[i] := the number of additional valid subarrays we can gain if the
    // restriction at index `i` is removed
    long[] gains = new long[n + 1];
    // conflicts[r] := left endpoints that conflict with subarrays ending in r
    List<Integer>[] conflicts = new List[n + 1];

    for (int i = 0; i <= n; ++i)
      conflicts[i] = new ArrayList<>();

    for (int[] pair : conflictingPairs) {
      final int a = pair[0];
      final int b = pair[1];
      conflicts[Math.max(a, b)].add(Math.min(a, b));
    }

    for (int right = 1; right <= n; ++right) {
      for (int left : conflicts[right]) {
        if (left > maxLeft) {
          secondMaxLeft = maxLeft;
          maxLeft = left;
        } else if (left > secondMaxLeft) {
          secondMaxLeft = left;
        }
      }
      // Subarrays [maxLeft + 1..right],
      //           [maxLeft + 2..right],
      //           ...
      //           [right..right] are valid.
      validSubarrays += right - maxLeft;
      // If we remove `maxLeft` (the most restrictive conflict), we gain
      // `maxLeft - secondMaxLeft` new subarrays:
      // [secondMaxLeft + 1..right],
      // [secondMaxLeft + 2..right],
      // ...
      // [maxLeft..right].
      gains[maxLeft] += maxLeft - secondMaxLeft;
    }

    return validSubarrays + Arrays.stream(gains).max().getAsLong();
  }
}
// code provided by PROGIEZ

3480. Maximize Subarrays After Removing One Conflicting Pair LeetCode Solution in Python

class Solution:
  def maxSubarrays(self, n: int, conflictingPairs: list[list[int]]) -> int:
    validSubarrays = 0
    maxLeft = 0
    secondMaxLeft = 0
    # gains[i] := the number of additional valid subarrays we can gain if the
    # restriction at index `i` is removed
    gains = [0] * (n + 1)
    # conflicts[r] := left endpoints that conflict with subarrays ending in r
    conflicts = [[] for _ in range(n + 1)]

    for a, b in conflictingPairs:
      conflicts[max(a, b)].append(min(a, b))

    for right in range(1, n + 1):
      for left in conflicts[right]:
        if left > maxLeft:
          secondMaxLeft = maxLeft
          maxLeft = left
        elif left > secondMaxLeft:
          secondMaxLeft = left
      # Subarrays [maxLeft + 1..right],
      #           [maxLeft + 2..right],
      #           ...
      #           [right..right] are valid.
      validSubarrays += right - maxLeft
      # If we remove `maxLeft` (the most restrictive conflict), we gain
      # `maxLeft - secondMaxLeft` new subarrays:
      # [secondMaxLeft + 1..right],
      # [secondMaxLeft + 2..right],
      # ...
      # [maxLeft..right].
      gains[maxLeft] += maxLeft - secondMaxLeft

    return validSubarrays + max(gains)
# code by PROGIEZ

Additional Resources

See also  900. RLE Iterator LeetCode Solution

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