From e2fc842a06a9beed5a92a9ff60486f358a6ba40e Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 26 Jul 2025 20:18:16 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3480 No.3480.Maximize Subarrays After Removing One Conflicting Pair --- .../README.md | 213 +++++++++++++++++- .../README_EN.md | 213 +++++++++++++++++- .../Solution.cpp | 34 +++ .../Solution.go | 33 +++ .../Solution.java | 33 +++ .../Solution.py | 21 ++ .../Solution.rs | 36 +++ .../Solution.ts | 32 +++ 8 files changed, 607 insertions(+), 8 deletions(-) create mode 100644 solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.cpp create mode 100644 solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.go create mode 100644 solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.java create mode 100644 solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.py create mode 100644 solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.rs create mode 100644 solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.ts diff --git a/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README.md b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README.md index 0426c3f26aa76..f888e89638b82 100644 --- a/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README.md +++ b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README.md @@ -82,32 +82,237 @@ tags: -### 方法一 +### 方法一:枚举 + 维护最小与次小值 + +我们把所有冲突对 $(a, b)$(假设 $a \lt b$)存入一个列表 $g$ 中,其中 $g[a]$ 表示所有与 $a$ 冲突的数 $b$ 的集合。 + +假设没有删除,那么我们可以倒序枚举每个子数组的左端点 $a$,那么其右端点的上界就是所有 $g[x \geq a]$ 中的最小值 $b_1$(不包括 $b_1$),对答案的贡献就是 $b_1 - a$。 + +如果我们删除了一个包含 $b_1$ 的冲突对,那么此时新的 $b_1$ 就是所有 $g[x \geq a]$ 中的次小值 $b_2$,其对答案新增的贡献为 $b_2 - b_1$。我们用一个数组 $\text{cnt}$ 来记录每个 $b_1$ 的新增贡献。 + +最终答案就是所有 $b_1 - a$ 的贡献加上 $\text{cnt}[b_1]$ 的最大值。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是冲突对的数量。 #### Python3 ```python - +class Solution: + def maxSubarrays(self, n: int, conflictingPairs: List[List[int]]) -> int: + g = [[] for _ in range(n + 1)] + for a, b in conflictingPairs: + if a > b: + a, b = b, a + g[a].append(b) + cnt = [0] * (n + 2) + ans = add = 0 + b1 = b2 = n + 1 + for a in range(n, 0, -1): + for b in g[a]: + if b < b1: + b2, b1 = b1, b + elif b < b2: + b2 = b + ans += b1 - a + cnt[b1] += b2 - b1 + add = max(add, cnt[b1]) + ans += add + return ans ``` #### Java ```java - +class Solution { + public long maxSubarrays(int n, int[][] conflictingPairs) { + List[] g = new List[n + 1]; + Arrays.setAll(g, k -> new ArrayList<>()); + for (int[] pair : conflictingPairs) { + int a = pair[0], b = pair[1]; + if (a > b) { + int c = a; + a = b; + b = c; + } + g[a].add(b); + } + long[] cnt = new long[n + 2]; + long ans = 0, add = 0; + int b1 = n + 1, b2 = n + 1; + for (int a = n; a > 0; --a) { + for (int b : g[a]) { + if (b < b1) { + b2 = b1; + b1 = b; + } else if (b < b2) { + b2 = b; + } + } + ans += b1 - a; + cnt[b1] += b2 - b1; + add = Math.max(add, cnt[b1]); + } + ans += add; + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long maxSubarrays(int n, vector>& conflictingPairs) { + vector> g(n + 1); + for (auto& pair : conflictingPairs) { + int a = pair[0], b = pair[1]; + if (a > b) { + swap(a, b); + } + g[a].push_back(b); + } + + vector cnt(n + 2, 0); + long long ans = 0, add = 0; + int b1 = n + 1, b2 = n + 1; + + for (int a = n; a > 0; --a) { + for (int b : g[a]) { + if (b < b1) { + b2 = b1; + b1 = b; + } else if (b < b2) { + b2 = b; + } + } + ans += b1 - a; + cnt[b1] += b2 - b1; + add = max(add, cnt[b1]); + } + + ans += add; + return ans; + } +}; ``` #### Go ```go +func maxSubarrays(n int, conflictingPairs [][]int) (ans int64) { + g := make([][]int, n+1) + for _, pair := range conflictingPairs { + a, b := pair[0], pair[1] + if a > b { + a, b = b, a + } + g[a] = append(g[a], b) + } + + cnt := make([]int64, n+2) + var add int64 + b1, b2 := n+1, n+1 + + for a := n; a > 0; a-- { + for _, b := range g[a] { + if b < b1 { + b2 = b1 + b1 = b + } else if b < b2 { + b2 = b + } + } + ans += int64(b1 - a) + cnt[b1] += int64(b2 - b1) + if cnt[b1] > add { + add = cnt[b1] + } + } + + ans += add + return ans +} +``` + +#### TypeScript + +```ts +function maxSubarrays(n: number, conflictingPairs: number[][]): number { + const g: number[][] = Array.from({ length: n + 1 }, () => []); + for (let [a, b] of conflictingPairs) { + if (a > b) { + [a, b] = [b, a]; + } + g[a].push(b); + } + + const cnt: number[] = Array(n + 2).fill(0); + let ans = 0, + add = 0; + let b1 = n + 1, + b2 = n + 1; + + for (let a = n; a > 0; a--) { + for (const b of g[a]) { + if (b < b1) { + b2 = b1; + b1 = b; + } else if (b < b2) { + b2 = b; + } + } + ans += b1 - a; + cnt[b1] += b2 - b1; + add = Math.max(add, cnt[b1]); + } + + ans += add; + return ans; +} +``` +#### Rust + +```rust +impl Solution { + pub fn max_subarrays(n: i32, conflicting_pairs: Vec>) -> i64 { + let mut g: Vec> = vec![vec![]; (n + 1) as usize]; + for pair in conflicting_pairs { + let mut a = pair[0]; + let mut b = pair[1]; + if a > b { + std::mem::swap(&mut a, &mut b); + } + g[a as usize].push(b); + } + + let mut cnt: Vec = vec![0; (n + 2) as usize]; + let mut ans = 0i64; + let mut add = 0i64; + let mut b1 = n + 1; + let mut b2 = n + 1; + + for a in (1..=n).rev() { + for &b in &g[a as usize] { + if b < b1 { + b2 = b1; + b1 = b; + } else if b < b2 { + b2 = b; + } + } + ans += (b1 - a) as i64; + cnt[b1 as usize] += (b2 - b1) as i64; + add = std::cmp::max(add, cnt[b1 as usize]); + } + + ans += add; + ans + } +} ``` diff --git a/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README_EN.md b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README_EN.md index 0f545a2a017c1..616df7b51ff1c 100644 --- a/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README_EN.md +++ b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/README_EN.md @@ -77,32 +77,237 @@ tags: -### Solution 1 +### Solution 1: Enumeration + Maintaining Minimum and Second Minimum Values + +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. #### Python3 ```python - +class Solution: + def maxSubarrays(self, n: int, conflictingPairs: List[List[int]]) -> int: + g = [[] for _ in range(n + 1)] + for a, b in conflictingPairs: + if a > b: + a, b = b, a + g[a].append(b) + cnt = [0] * (n + 2) + ans = add = 0 + b1 = b2 = n + 1 + for a in range(n, 0, -1): + for b in g[a]: + if b < b1: + b2, b1 = b1, b + elif b < b2: + b2 = b + ans += b1 - a + cnt[b1] += b2 - b1 + add = max(add, cnt[b1]) + ans += add + return ans ``` #### Java ```java - +class Solution { + public long maxSubarrays(int n, int[][] conflictingPairs) { + List[] g = new List[n + 1]; + Arrays.setAll(g, k -> new ArrayList<>()); + for (int[] pair : conflictingPairs) { + int a = pair[0], b = pair[1]; + if (a > b) { + int c = a; + a = b; + b = c; + } + g[a].add(b); + } + long[] cnt = new long[n + 2]; + long ans = 0, add = 0; + int b1 = n + 1, b2 = n + 1; + for (int a = n; a > 0; --a) { + for (int b : g[a]) { + if (b < b1) { + b2 = b1; + b1 = b; + } else if (b < b2) { + b2 = b; + } + } + ans += b1 - a; + cnt[b1] += b2 - b1; + add = Math.max(add, cnt[b1]); + } + ans += add; + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long maxSubarrays(int n, vector>& conflictingPairs) { + vector> g(n + 1); + for (auto& pair : conflictingPairs) { + int a = pair[0], b = pair[1]; + if (a > b) { + swap(a, b); + } + g[a].push_back(b); + } + + vector cnt(n + 2, 0); + long long ans = 0, add = 0; + int b1 = n + 1, b2 = n + 1; + + for (int a = n; a > 0; --a) { + for (int b : g[a]) { + if (b < b1) { + b2 = b1; + b1 = b; + } else if (b < b2) { + b2 = b; + } + } + ans += b1 - a; + cnt[b1] += b2 - b1; + add = max(add, cnt[b1]); + } + + ans += add; + return ans; + } +}; ``` #### Go ```go +func maxSubarrays(n int, conflictingPairs [][]int) (ans int64) { + g := make([][]int, n+1) + for _, pair := range conflictingPairs { + a, b := pair[0], pair[1] + if a > b { + a, b = b, a + } + g[a] = append(g[a], b) + } + + cnt := make([]int64, n+2) + var add int64 + b1, b2 := n+1, n+1 + + for a := n; a > 0; a-- { + for _, b := range g[a] { + if b < b1 { + b2 = b1 + b1 = b + } else if b < b2 { + b2 = b + } + } + ans += int64(b1 - a) + cnt[b1] += int64(b2 - b1) + if cnt[b1] > add { + add = cnt[b1] + } + } + + ans += add + return ans +} +``` + +#### TypeScript + +```ts +function maxSubarrays(n: number, conflictingPairs: number[][]): number { + const g: number[][] = Array.from({ length: n + 1 }, () => []); + for (let [a, b] of conflictingPairs) { + if (a > b) { + [a, b] = [b, a]; + } + g[a].push(b); + } + + const cnt: number[] = Array(n + 2).fill(0); + let ans = 0, + add = 0; + let b1 = n + 1, + b2 = n + 1; + + for (let a = n; a > 0; a--) { + for (const b of g[a]) { + if (b < b1) { + b2 = b1; + b1 = b; + } else if (b < b2) { + b2 = b; + } + } + ans += b1 - a; + cnt[b1] += b2 - b1; + add = Math.max(add, cnt[b1]); + } + + ans += add; + return ans; +} +``` +#### Rust + +```rust +impl Solution { + pub fn max_subarrays(n: i32, conflicting_pairs: Vec>) -> i64 { + let mut g: Vec> = vec![vec![]; (n + 1) as usize]; + for pair in conflicting_pairs { + let mut a = pair[0]; + let mut b = pair[1]; + if a > b { + std::mem::swap(&mut a, &mut b); + } + g[a as usize].push(b); + } + + let mut cnt: Vec = vec![0; (n + 2) as usize]; + let mut ans = 0i64; + let mut add = 0i64; + let mut b1 = n + 1; + let mut b2 = n + 1; + + for a in (1..=n).rev() { + for &b in &g[a as usize] { + if b < b1 { + b2 = b1; + b1 = b; + } else if b < b2 { + b2 = b; + } + } + ans += (b1 - a) as i64; + cnt[b1 as usize] += (b2 - b1) as i64; + add = std::cmp::max(add, cnt[b1 as usize]); + } + + ans += add; + ans + } +} ``` diff --git a/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.cpp b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.cpp new file mode 100644 index 0000000000000..591c5220a9d56 --- /dev/null +++ b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + long long maxSubarrays(int n, vector>& conflictingPairs) { + vector> g(n + 1); + for (auto& pair : conflictingPairs) { + int a = pair[0], b = pair[1]; + if (a > b) { + swap(a, b); + } + g[a].push_back(b); + } + + vector cnt(n + 2, 0); + long long ans = 0, add = 0; + int b1 = n + 1, b2 = n + 1; + + for (int a = n; a > 0; --a) { + for (int b : g[a]) { + if (b < b1) { + b2 = b1; + b1 = b; + } else if (b < b2) { + b2 = b; + } + } + ans += b1 - a; + cnt[b1] += b2 - b1; + add = max(add, cnt[b1]); + } + + ans += add; + return ans; + } +}; diff --git a/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.go b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.go new file mode 100644 index 0000000000000..d5c5bcfad5f5d --- /dev/null +++ b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.go @@ -0,0 +1,33 @@ +func maxSubarrays(n int, conflictingPairs [][]int) (ans int64) { + g := make([][]int, n+1) + for _, pair := range conflictingPairs { + a, b := pair[0], pair[1] + if a > b { + a, b = b, a + } + g[a] = append(g[a], b) + } + + cnt := make([]int64, n+2) + var add int64 + b1, b2 := n+1, n+1 + + for a := n; a > 0; a-- { + for _, b := range g[a] { + if b < b1 { + b2 = b1 + b1 = b + } else if b < b2 { + b2 = b + } + } + ans += int64(b1 - a) + cnt[b1] += int64(b2 - b1) + if cnt[b1] > add { + add = cnt[b1] + } + } + + ans += add + return ans +} diff --git a/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.java b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.java new file mode 100644 index 0000000000000..3b6fe318af668 --- /dev/null +++ b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.java @@ -0,0 +1,33 @@ +class Solution { + public long maxSubarrays(int n, int[][] conflictingPairs) { + List[] g = new List[n + 1]; + Arrays.setAll(g, k -> new ArrayList<>()); + for (int[] pair : conflictingPairs) { + int a = pair[0], b = pair[1]; + if (a > b) { + int c = a; + a = b; + b = c; + } + g[a].add(b); + } + long[] cnt = new long[n + 2]; + long ans = 0, add = 0; + int b1 = n + 1, b2 = n + 1; + for (int a = n; a > 0; --a) { + for (int b : g[a]) { + if (b < b1) { + b2 = b1; + b1 = b; + } else if (b < b2) { + b2 = b; + } + } + ans += b1 - a; + cnt[b1] += b2 - b1; + add = Math.max(add, cnt[b1]); + } + ans += add; + return ans; + } +} diff --git a/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.py b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.py new file mode 100644 index 0000000000000..deebf213f7acd --- /dev/null +++ b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.py @@ -0,0 +1,21 @@ +class Solution: + def maxSubarrays(self, n: int, conflictingPairs: List[List[int]]) -> int: + g = [[] for _ in range(n + 1)] + for a, b in conflictingPairs: + if a > b: + a, b = b, a + g[a].append(b) + cnt = [0] * (n + 2) + ans = add = 0 + b1 = b2 = n + 1 + for a in range(n, 0, -1): + for b in g[a]: + if b < b1: + b2, b1 = b1, b + elif b < b2: + b2 = b + ans += b1 - a + cnt[b1] += b2 - b1 + add = max(add, cnt[b1]) + ans += add + return ans diff --git a/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.rs b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.rs new file mode 100644 index 0000000000000..a3e9c9d49ef30 --- /dev/null +++ b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.rs @@ -0,0 +1,36 @@ +impl Solution { + pub fn max_subarrays(n: i32, conflicting_pairs: Vec>) -> i64 { + let mut g: Vec> = vec![vec![]; (n + 1) as usize]; + for pair in conflicting_pairs { + let mut a = pair[0]; + let mut b = pair[1]; + if a > b { + std::mem::swap(&mut a, &mut b); + } + g[a as usize].push(b); + } + + let mut cnt: Vec = vec![0; (n + 2) as usize]; + let mut ans = 0i64; + let mut add = 0i64; + let mut b1 = n + 1; + let mut b2 = n + 1; + + for a in (1..=n).rev() { + for &b in &g[a as usize] { + if b < b1 { + b2 = b1; + b1 = b; + } else if b < b2 { + b2 = b; + } + } + ans += (b1 - a) as i64; + cnt[b1 as usize] += (b2 - b1) as i64; + add = std::cmp::max(add, cnt[b1 as usize]); + } + + ans += add; + ans + } +} diff --git a/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.ts b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.ts new file mode 100644 index 0000000000000..a8a7e5e47039b --- /dev/null +++ b/solution/3400-3499/3480.Maximize Subarrays After Removing One Conflicting Pair/Solution.ts @@ -0,0 +1,32 @@ +function maxSubarrays(n: number, conflictingPairs: number[][]): number { + const g: number[][] = Array.from({ length: n + 1 }, () => []); + for (let [a, b] of conflictingPairs) { + if (a > b) { + [a, b] = [b, a]; + } + g[a].push(b); + } + + const cnt: number[] = Array(n + 2).fill(0); + let ans = 0, + add = 0; + let b1 = n + 1, + b2 = n + 1; + + for (let a = n; a > 0; a--) { + for (const b of g[a]) { + if (b < b1) { + b2 = b1; + b1 = b; + } else if (b < b2) { + b2 = b; + } + } + ans += b1 - a; + cnt[b1] += b2 - b1; + add = Math.max(add, cnt[b1]); + } + + ans += add; + return ans; +}