Skip to content

Commit 721ac60

Browse files
authored
feat: add solutions to lc problem: No.3480 (#4598)
No.3480.Maximize Subarrays After Removing One Conflicting Pair
1 parent 57d33e8 commit 721ac60

File tree

8 files changed

+607
-8
lines changed

8 files changed

+607
-8
lines changed

solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README.md

Lines changed: 209 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,32 +82,237 @@ tags:
8282

8383
<!-- solution:start -->
8484

85-
### 方法一
85+
### 方法一:枚举 + 维护最小与次小值
86+
87+
我们把所有冲突对 $(a, b)$(假设 $a \lt b$)存入一个列表 $g$ 中,其中 $g[a]$ 表示所有与 $a$ 冲突的数 $b$ 的集合。
88+
89+
假设没有删除,那么我们可以倒序枚举每个子数组的左端点 $a$,那么其右端点的上界就是所有 $g[x \geq a]$ 中的最小值 $b_1$(不包括 $b_1$),对答案的贡献就是 $b_1 - a$。
90+
91+
如果我们删除了一个包含 $b_1$ 的冲突对,那么此时新的 $b_1$ 就是所有 $g[x \geq a]$ 中的次小值 $b_2$,其对答案新增的贡献为 $b_2 - b_1$。我们用一个数组 $\text{cnt}$ 来记录每个 $b_1$ 的新增贡献。
92+
93+
最终答案就是所有 $b_1 - a$ 的贡献加上 $\text{cnt}[b_1]$ 的最大值。
94+
95+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是冲突对的数量。
8696

8797
<!-- tabs:start -->
8898

8999
#### Python3
90100

91101
```python
92-
102+
class Solution:
103+
def maxSubarrays(self, n: int, conflictingPairs: List[List[int]]) -> int:
104+
g = [[] for _ in range(n + 1)]
105+
for a, b in conflictingPairs:
106+
if a > b:
107+
a, b = b, a
108+
g[a].append(b)
109+
cnt = [0] * (n + 2)
110+
ans = add = 0
111+
b1 = b2 = n + 1
112+
for a in range(n, 0, -1):
113+
for b in g[a]:
114+
if b < b1:
115+
b2, b1 = b1, b
116+
elif b < b2:
117+
b2 = b
118+
ans += b1 - a
119+
cnt[b1] += b2 - b1
120+
add = max(add, cnt[b1])
121+
ans += add
122+
return ans
93123
```
94124

95125
#### Java
96126

97127
```java
98-
128+
class Solution {
129+
public long maxSubarrays(int n, int[][] conflictingPairs) {
130+
List<Integer>[] g = new List[n + 1];
131+
Arrays.setAll(g, k -> new ArrayList<>());
132+
for (int[] pair : conflictingPairs) {
133+
int a = pair[0], b = pair[1];
134+
if (a > b) {
135+
int c = a;
136+
a = b;
137+
b = c;
138+
}
139+
g[a].add(b);
140+
}
141+
long[] cnt = new long[n + 2];
142+
long ans = 0, add = 0;
143+
int b1 = n + 1, b2 = n + 1;
144+
for (int a = n; a > 0; --a) {
145+
for (int b : g[a]) {
146+
if (b < b1) {
147+
b2 = b1;
148+
b1 = b;
149+
} else if (b < b2) {
150+
b2 = b;
151+
}
152+
}
153+
ans += b1 - a;
154+
cnt[b1] += b2 - b1;
155+
add = Math.max(add, cnt[b1]);
156+
}
157+
ans += add;
158+
return ans;
159+
}
160+
}
99161
```
100162

101163
#### C++
102164

103165
```cpp
104-
166+
class Solution {
167+
public:
168+
long long maxSubarrays(int n, vector<vector<int>>& conflictingPairs) {
169+
vector<vector<int>> g(n + 1);
170+
for (auto& pair : conflictingPairs) {
171+
int a = pair[0], b = pair[1];
172+
if (a > b) {
173+
swap(a, b);
174+
}
175+
g[a].push_back(b);
176+
}
177+
178+
vector<long long> cnt(n + 2, 0);
179+
long long ans = 0, add = 0;
180+
int b1 = n + 1, b2 = n + 1;
181+
182+
for (int a = n; a > 0; --a) {
183+
for (int b : g[a]) {
184+
if (b < b1) {
185+
b2 = b1;
186+
b1 = b;
187+
} else if (b < b2) {
188+
b2 = b;
189+
}
190+
}
191+
ans += b1 - a;
192+
cnt[b1] += b2 - b1;
193+
add = max(add, cnt[b1]);
194+
}
195+
196+
ans += add;
197+
return ans;
198+
}
199+
};
105200
```
106201

107202
#### Go
108203

109204
```go
205+
func maxSubarrays(n int, conflictingPairs [][]int) (ans int64) {
206+
g := make([][]int, n+1)
207+
for _, pair := range conflictingPairs {
208+
a, b := pair[0], pair[1]
209+
if a > b {
210+
a, b = b, a
211+
}
212+
g[a] = append(g[a], b)
213+
}
214+
215+
cnt := make([]int64, n+2)
216+
var add int64
217+
b1, b2 := n+1, n+1
218+
219+
for a := n; a > 0; a-- {
220+
for _, b := range g[a] {
221+
if b < b1 {
222+
b2 = b1
223+
b1 = b
224+
} else if b < b2 {
225+
b2 = b
226+
}
227+
}
228+
ans += int64(b1 - a)
229+
cnt[b1] += int64(b2 - b1)
230+
if cnt[b1] > add {
231+
add = cnt[b1]
232+
}
233+
}
234+
235+
ans += add
236+
return ans
237+
}
238+
```
239+
240+
#### TypeScript
241+
242+
```ts
243+
function maxSubarrays(n: number, conflictingPairs: number[][]): number {
244+
const g: number[][] = Array.from({ length: n + 1 }, () => []);
245+
for (let [a, b] of conflictingPairs) {
246+
if (a > b) {
247+
[a, b] = [b, a];
248+
}
249+
g[a].push(b);
250+
}
251+
252+
const cnt: number[] = Array(n + 2).fill(0);
253+
let ans = 0,
254+
add = 0;
255+
let b1 = n + 1,
256+
b2 = n + 1;
257+
258+
for (let a = n; a > 0; a--) {
259+
for (const b of g[a]) {
260+
if (b < b1) {
261+
b2 = b1;
262+
b1 = b;
263+
} else if (b < b2) {
264+
b2 = b;
265+
}
266+
}
267+
ans += b1 - a;
268+
cnt[b1] += b2 - b1;
269+
add = Math.max(add, cnt[b1]);
270+
}
271+
272+
ans += add;
273+
return ans;
274+
}
275+
```
110276

277+
#### Rust
278+
279+
```rust
280+
impl Solution {
281+
pub fn max_subarrays(n: i32, conflicting_pairs: Vec<Vec<i32>>) -> i64 {
282+
let mut g: Vec<Vec<i32>> = vec![vec![]; (n + 1) as usize];
283+
for pair in conflicting_pairs {
284+
let mut a = pair[0];
285+
let mut b = pair[1];
286+
if a > b {
287+
std::mem::swap(&mut a, &mut b);
288+
}
289+
g[a as usize].push(b);
290+
}
291+
292+
let mut cnt: Vec<i64> = vec![0; (n + 2) as usize];
293+
let mut ans = 0i64;
294+
let mut add = 0i64;
295+
let mut b1 = n + 1;
296+
let mut b2 = n + 1;
297+
298+
for a in (1..=n).rev() {
299+
for &b in &g[a as usize] {
300+
if b < b1 {
301+
b2 = b1;
302+
b1 = b;
303+
} else if b < b2 {
304+
b2 = b;
305+
}
306+
}
307+
ans += (b1 - a) as i64;
308+
cnt[b1 as usize] += (b2 - b1) as i64;
309+
add = std::cmp::max(add, cnt[b1 as usize]);
310+
}
311+
312+
ans += add;
313+
ans
314+
}
315+
}
111316
```
112317

113318
<!-- tabs:end -->

0 commit comments

Comments
 (0)