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:
[2, 3] from conflictingPairs. Now, conflictingPairs = [[1, 4]].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].conflictingPairs is 9.Example 2:
Input: n = 5, conflictingPairs = [[1,2],[2,5],[3,5]]
Output: 12
Explanation:
[1, 2] from conflictingPairs. Now, conflictingPairs = [[2, 5], [3, 5]].nums where [2, 5] and [3, 5] do not appear together.conflictingPairs is 12.Constraints:
2 <= n <= 1051 <= conflictingPairs.length <= 2 * nconflictingPairs[i].length == 21 <= conflictingPairs[i][j] <= nconflictingPairs[i][0] != conflictingPairs[i][1]f[i] (where i = 1, 2, 3, ..., n) be the end index of the longest valid subarray (without any conflicting pair) starting at index i.sigma(f[i] - i + 1) for i in [1..n], which simplifies to: sigma(f[i]) - n * (n + 1) / 2 + n.f[i].(x, y) with x < y: 1. Sort the conflicting pairs by y values in non-increasing order. 2. Update each prefix of the f array accordingly.4 [[2,3],[1,4]]
5 [[1,2],[2,5],[3,5]]