Skip to content

Commit 0dc7c5a

Browse files
committed
Add problem 2382: Maximum Segment Sum After Removals
1 parent 07caece commit 0dc7c5a

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,7 @@ pub mod problem_2375_construct_smallest_number_from_di_string;
17721772
pub mod problem_2379_minimum_recolors_to_get_k_consecutive_black_blocks;
17731773
pub mod problem_2380_time_needed_to_rearrange_a_binary_string;
17741774
pub mod problem_2381_shifting_letters_ii;
1775+
pub mod problem_2382_maximum_segment_sum_after_removals;
17751776

17761777
#[cfg(test)]
17771778
mod test_utilities;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
pub mod reversed_iteration;
2+
3+
pub trait Solution {
4+
fn maximum_segment_sum(nums: Vec<i32>, remove_queries: Vec<i32>) -> Vec<i64>;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
(
14+
(&[1, 2, 5, 6, 1] as &[_], &[0, 3, 2, 4, 1] as &[_]),
15+
&[14_i64, 7, 2, 2, 0] as &[_],
16+
),
17+
((&[3, 2, 11, 1], &[3, 2, 1, 0]), &[16, 5, 3, 0]),
18+
(
19+
(
20+
&[
21+
244, 19, 445, 671, 801, 103, 291, 335, 781, 33, 51, 789, 746, 510, 38, 7, 529, 905,
22+
],
23+
&[4, 8, 11, 12, 1, 5, 0, 9, 6, 17, 3, 15, 14, 7, 2, 13, 16, 10],
24+
),
25+
&[
26+
5118, 3608, 2735, 1989, 1989, 1989, 1989, 1989, 1989, 1116, 1084, 548, 529, 529, 529, 529, 51, 0,
27+
],
28+
),
29+
];
30+
31+
for ((nums, remove_queries), expected) in test_cases {
32+
assert_eq!(S::maximum_segment_sum(nums.to_vec(), remove_queries.to_vec()), expected);
33+
}
34+
}
35+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::cell::Cell;
6+
use std::iter;
7+
8+
struct Item {
9+
end_index: Cell<usize>,
10+
sum: Cell<u64>,
11+
}
12+
13+
impl Solution {
14+
fn filter(item: &Item) -> Option<(&Item, u64)> {
15+
let item_sum = item.sum.get();
16+
17+
(item_sum != 0).then_some((item, item_sum))
18+
}
19+
20+
pub fn maximum_segment_sum(nums: Vec<i32>, remove_queries: Vec<i32>) -> Vec<i64> {
21+
let n = remove_queries.len();
22+
23+
let items = iter::repeat_with(|| Item {
24+
sum: Cell::new(0),
25+
end_index: Cell::new(0),
26+
})
27+
.take(n)
28+
.collect::<Box<_>>();
29+
30+
let mut result = vec![0; n];
31+
let mut max_sum = 0;
32+
33+
result
34+
.iter_mut()
35+
.zip(&remove_queries)
36+
.rev()
37+
.for_each(|(target, &index)| {
38+
*target = max_sum as _;
39+
40+
let mut index = index as u32 as usize;
41+
let mut end = &items[index];
42+
let mut sum = u64::from(nums[index] as u32);
43+
let other_end;
44+
45+
'block: {
46+
let (other, other_sum) = match (
47+
items.get(index.wrapping_sub(1)).and_then(Self::filter),
48+
items.get(index + 1).and_then(Self::filter),
49+
) {
50+
(None, None) => {
51+
other_end = end;
52+
53+
break 'block;
54+
}
55+
(None, Some((other, other_sum))) | (Some((other, other_sum)), None) => (other, other_sum),
56+
(Some((left, left_sum)), Some((right, right_sum))) => {
57+
index = left.end_index.get();
58+
end = &items[index];
59+
sum += left_sum;
60+
61+
(right, right_sum)
62+
}
63+
};
64+
65+
let other_end_index = other.end_index.get();
66+
67+
other_end = &items[other_end_index];
68+
69+
sum += other_sum;
70+
71+
end.end_index.set(other_end_index);
72+
end.sum.set(sum);
73+
}
74+
75+
other_end.end_index.set(index);
76+
other_end.sum.set(sum);
77+
78+
max_sum = max_sum.max(sum);
79+
});
80+
81+
result
82+
}
83+
}
84+
85+
// ------------------------------------------------------ snip ------------------------------------------------------ //
86+
87+
impl super::Solution for Solution {
88+
fn maximum_segment_sum(nums: Vec<i32>, remove_queries: Vec<i32>) -> Vec<i64> {
89+
Self::maximum_segment_sum(nums, remove_queries)
90+
}
91+
}
92+
93+
#[cfg(test)]
94+
mod tests {
95+
#[test]
96+
fn test_solution() {
97+
super::super::tests::run::<super::Solution>();
98+
}
99+
}

0 commit comments

Comments
 (0)