|
| 1 | +<p>You are given an integer <code>n</code> which represents an array <code>nums</code> containing the numbers from 1 to <code>n</code> in order. Additionally, you are given a 2D array <code>conflictingPairs</code>, where <code>conflictingPairs[i] = [a, b]</code> indicates that <code>a</code> and <code>b</code> form a conflicting pair.</p> |
| 2 | + |
| 3 | +<p>Remove <strong>exactly</strong> one element from <code>conflictingPairs</code>. Afterward, count the number of <span data-keyword="subarray-nonempty">non-empty subarrays</span> of <code>nums</code> which do not contain both <code>a</code> and <code>b</code> for any remaining conflicting pair <code>[a, b]</code>.</p> |
| 4 | + |
| 5 | +<p>Return the <strong>maximum</strong> number of subarrays possible after removing <strong>exactly</strong> one conflicting pair.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<div class="example-block"> |
| 11 | +<p><strong>Input:</strong> <span class="example-io">n = 4, conflictingPairs = [[2,3],[1,4]]</span></p> |
| 12 | + |
| 13 | +<p><strong>Output:</strong> <span class="example-io">9</span></p> |
| 14 | + |
| 15 | +<p><strong>Explanation:</strong></p> |
| 16 | + |
| 17 | +<ul> |
| 18 | + <li>Remove <code>[2, 3]</code> from <code>conflictingPairs</code>. Now, <code>conflictingPairs = [[1, 4]]</code>.</li> |
| 19 | + <li>There are 9 subarrays in <code>nums</code> where <code>[1, 4]</code> do not appear together. They are <code>[1]</code>, <code>[2]</code>, <code>[3]</code>, <code>[4]</code>, <code>[1, 2]</code>, <code>[2, 3]</code>, <code>[3, 4]</code>, <code>[1, 2, 3]</code> and <code>[2, 3, 4]</code>.</li> |
| 20 | + <li>The maximum number of subarrays we can achieve after removing one element from <code>conflictingPairs</code> is 9.</li> |
| 21 | +</ul> |
| 22 | +</div> |
| 23 | + |
| 24 | +<p><strong class="example">Example 2:</strong></p> |
| 25 | + |
| 26 | +<div class="example-block"> |
| 27 | +<p><strong>Input:</strong> <span class="example-io">n = 5, conflictingPairs = [[1,2],[2,5],[3,5]]</span></p> |
| 28 | + |
| 29 | +<p><strong>Output:</strong> <span class="example-io">12</span></p> |
| 30 | + |
| 31 | +<p><strong>Explanation:</strong></p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li>Remove <code>[1, 2]</code> from <code>conflictingPairs</code>. Now, <code>conflictingPairs = [[2, 5], [3, 5]]</code>.</li> |
| 35 | + <li>There are 12 subarrays in <code>nums</code> where <code>[2, 5]</code> and <code>[3, 5]</code> do not appear together.</li> |
| 36 | + <li>The maximum number of subarrays we can achieve after removing one element from <code>conflictingPairs</code> is 12.</li> |
| 37 | +</ul> |
| 38 | +</div> |
| 39 | + |
| 40 | +<p> </p> |
| 41 | +<p><strong>Constraints:</strong></p> |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li><code>2 <= n <= 10<sup>5</sup></code></li> |
| 45 | + <li><code>1 <= conflictingPairs.length <= 2 * n</code></li> |
| 46 | + <li><code>conflictingPairs[i].length == 2</code></li> |
| 47 | + <li><code>1 <= conflictingPairs[i][j] <= n</code></li> |
| 48 | + <li><code>conflictingPairs[i][0] != conflictingPairs[i][1]</code></li> |
| 49 | +</ul> |
0 commit comments