3480. 删除一个冲突对后最大子数组数目
This commit is contained in:
parent
0af11c087d
commit
911aa923cb
35
src/main.rs
35
src/main.rs
|
@ -1,25 +1,30 @@
|
||||||
struct Solution;
|
struct Solution;
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn max_sum(mut nums: Vec<i32>) -> i32 {
|
pub fn max_subarrays(n: i32, mut conflicting_pairs: Vec<Vec<i32>>) -> i64 {
|
||||||
nums.sort_by(|a,b| b.cmp(a));
|
let mut arr: Vec<Vec<i32>> = vec![vec![]; (n + 1) as usize];
|
||||||
let mut last = nums[0];
|
for pair in conflicting_pairs.iter_mut() {
|
||||||
let mut ans = nums[0];
|
if pair[0] > pair[1] {
|
||||||
for i in 1..nums.len() {
|
pair.swap(0, 1);
|
||||||
let t = nums[i];
|
|
||||||
if t <= 0 {
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
if t == last {
|
arr[pair[0] as usize].push(pair[1]);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
last = t;
|
|
||||||
ans += t;
|
|
||||||
}
|
}
|
||||||
ans
|
let mut ans: i64 = 0;
|
||||||
|
let mut extra: Vec<i64> = vec![0; (n + 2) as usize];
|
||||||
|
let mut b: Vec<i64> = vec![(n + 1) as i64; (n + 1) as usize];
|
||||||
|
for i in (1..=n as usize).rev() {
|
||||||
|
b.extend(arr[i].iter().map(|&x| x as i64));
|
||||||
|
b.sort_unstable();
|
||||||
|
b.truncate(2);
|
||||||
|
|
||||||
|
ans += (b[0] as i64) - (i as i64);
|
||||||
|
extra[b[0] as usize] += b[1] - b[0];
|
||||||
|
}
|
||||||
|
ans + *extra.iter().max().unwrap() as i64
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let sl = Solution::max_sum(vec![-1,-2]);
|
let sl = Solution::max_subarrays(5, vec![vec![1, 2], vec![2, 5], vec![3, 5]]);
|
||||||
println!("{}", sl);
|
println!("{}", sl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue