3480. Maximize Subarrays After Removing One Conflicting Pair
Description
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]fromconflictingPairs. Now,conflictingPairs = [[1, 4]]. - There are 9 subarrays in
numswhere[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
conflictingPairsis 9.
Example 2:
Input: n = 5, conflictingPairs = [[1,2],[2,5],[3,5]]
Output: 12
Explanation:
- Remove
[1, 2]fromconflictingPairs. Now,conflictingPairs = [[2, 5], [3, 5]]. - There are 12 subarrays in
numswhere[2, 5]and[3, 5]do not appear together. - The maximum number of subarrays we can achieve after removing one element from
conflictingPairsis 12.
Constraints:
2 <= n <= 1051 <= conflictingPairs.length <= 2 * nconflictingPairs[i].length == 21 <= conflictingPairs[i][j] <= nconflictingPairs[i][0] != conflictingPairs[i][1]
Solutions
Solution 1: Enumeration + Maintaining Minimum and Second Minimum Values
Thinking
A valid subarray cannot contain both ends of any conflicting pair. We delete exactly one pair and want as many valid subarrays as possible. The scan must be linear.
With no deletion, a left end \(a\) cannot reach the smallest conflicting right end \(b_1\) seen from \(a\) rightward. It contributes \(b_1-a\).
Deleting the pair that created \(b_1\) replaces it by the second smallest \(b_2\) and adds \(b_2-b_1\). Those extras are grouped by \(b_1\); we add the best extra to the no-deletion total.
We store all conflicting pairs \((a, b)\) (assuming \(a < b\)) in a list \(g\), where \(g[a]\) represents the set of all numbers \(b\) that conflict with \(a\).
If no deletion occurs, we can enumerate each subarray's left endpoint \(a\) in reverse order. The upper bound of its right endpoint is the minimum value \(b_1\) among all \(g[x \geq a]\) (excluding \(b_1\)), and the contribution to the answer is \(b_1 - a\).
If we delete a conflicting pair containing \(b_1\), then the new \(b_1\) becomes the second minimum value \(b_2\) among all \(g[x \geq a]\), and its additional contribution to the answer is \(b_2 - b_1\). We use an array \(\text{cnt}\) to record the additional contribution for each \(b_1\).
The final answer is the sum of all \(b_1 - a\) contributions plus the maximum value of \(\text{cnt}[b_1]\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the number of conflicting pairs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |