Skip to content

Commit 253ddab

Browse files
committed
Sync LeetCode submission Runtime - 975 ms (44.44%), Memory - 85.4 MB (92.31%)
1 parent c95dec8 commit 253ddab

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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>&nbsp;</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>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
45+
<li><code>1 &lt;= conflictingPairs.length &lt;= 2 * n</code></li>
46+
<li><code>conflictingPairs[i].length == 2</code></li>
47+
<li><code>1 &lt;= conflictingPairs[i][j] &lt;= n</code></li>
48+
<li><code>conflictingPairs[i][0] != conflictingPairs[i][1]</code></li>
49+
</ul>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def maxSubarrays(self, n: int, conflictingPairs: List[List[int]]) -> int:
3+
bMin1 = [2**31 - 1] * (n + 1)
4+
bMin2 = [2**31 - 1] * (n + 1)
5+
for pair in conflictingPairs:
6+
a = min(pair[0], pair[1])
7+
b = max(pair[0], pair[1])
8+
if bMin1[a] > b:
9+
bMin2[a] = bMin1[a]
10+
bMin1[a] = b
11+
elif bMin2[a] > b:
12+
bMin2[a] = b
13+
res = 0
14+
ib1 = n
15+
b2 = 0x3FFFFFFF
16+
delCount = [0] * (n + 1)
17+
for i in range(n, 0, -1):
18+
if bMin1[ib1] > bMin1[i]:
19+
b2 = min(b2, bMin1[ib1])
20+
ib1 = i
21+
else:
22+
b2 = min(b2, bMin1[i])
23+
res += min(bMin1[ib1], n + 1) - i
24+
delCount[ib1] += min(min(b2, bMin2[ib1]), n + 1) - min(
25+
bMin1[ib1], n + 1
26+
)
27+
return res + max(delCount)

0 commit comments

Comments
 (0)