LeetCode每日一题(2382. Maximum Segment Sum After Removals)

给定两个数组nums和removeQueries,你需要在每次移除一个元素后计算最大段和。返回一个数组,其中answer[i]是进行第i次移除后的最大段和。题目涉及动态维护数组段和,使用前缀和策略进行优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are given two 0-indexed integer arrays nums and removeQueries, both of length n. For the ith query, the element in nums at the index removeQueries[i] is removed, splitting nums into different segments.

A segment is a contiguous sequence of positive integers in nums. A segment sum is the sum of every element in a segment.

Return an integer array answer, of length n, where answer[i] is the maximum segment sum after applying the ith removal.

Note: The same index will not be removed more than once.

Example 1:

Input: nums = [1,2,5,6,1], removeQueries = [0,3,2,4,1]
Output: [14,7,2,2,0]

Explanation: Using 0 to indicate a removed element, the answer is as follows:
Query 1: Remove the 0th element, nums becomes [0,2,5,6,1] and the maximum segment sum is 14 for segment [2,5,6,1].
Query 2: Remove the 3rd element, nums becomes [0,2,5,0,1] and the maximum segment sum is 7 for segment [2,5].
Query 3: Remove the 2nd element, nums becomes [0,2,0,0,1] and the maximum segment sum is 2 for segment [2].
Query 4: Remove the 4th element, nums becomes [0,2,0,0,0] and the maximum segment sum is 2 for segment [2].
Query 5: Remove the 1st element, nums becomes [0,0,0,0,0] and the maximum segment sum is 0, since there are no segments.
Finally, we return [14,7,2,2,0].

Example 2:

Input: nums = [3,2,11,1], removeQueries = [3,2,1,0]
Output: [16,5,3,0]

Explanation: Using 0 to indicate a removed element, the answer is as follows:
Query 1: Remove the 3rd element, nums becomes [3,2,11,0] and the maximum segment sum is 16 for segment [3,2,11].
Query 2: Remove the 2nd element, nums becomes [3,2,0,0] and the maximum segment sum is 5 for segment [3,2].
Query 3: Remove the 1st element, nums becomes [3,0,0,0] and the maximum segment sum is 3 for segment [3].
Query 4: Remove the 0th element, nums becomes [0,0,0,0] and the maximum segment sum is 0, since there are no segments.
Finally, we return [16,5,3,0].

Constraints:

  • n == nums.length == removeQueries.length
  • 1 <= n <= 105
  • 1 <= nums[i] <= 109
  • 0 <= removeQueries[i] < n
  • All the values of removeQueries are unique.

好想不好做。

因为 remove_queries 的 length 与 nums 的一样, 并且 remove_queries 中没有重复的元素, 所以最后一步一定所有 segments 都是空的, 这样最大值就是 0, 再往回推一步,一定是将 nums[remove_queries[last]]放到某一个 segments 中, 因为 nums 都是>0 的, 所以此时的 max 一定是 nums[remove_queries[last]]。再往前推一步就没有这么简单了, 但是我们按这种反向思路来思考, 我们倒序遍历 remove_queries 其实就是一个把 segments 进行粘合的一个过程, 我们添加 remove_queries[i]到 segments 中,其实就是把当前 remove_queries[i]左侧的 segment 和右侧的 segment 进行粘合。对 segment sum 的计算我们可以利用 prefix sum 进行计算。



impl Solution {
    pub fn maximum_segment_sum(nums: Vec<i32>, remove_queries: Vec<i32>) -> Vec<i64> {
        let mut prefix: Vec<i64> = vec![0];
        let mut sum = 0i64;
        for &n in &nums {
            sum += n as i64;
            prefix.push(sum);
        }
        let mut indices: Vec<i32> = (0..nums.len() as i32).collect();
        let mut max = 0i64;
        let mut ans = Vec::new();
        for q in remove_queries.into_iter().rev() {
            let i = indices.binary_search(&q).unwrap();
            let l = if i == 0 { -1 } else { indices[i - 1] };
            let r = if i == indices.len() - 1 {
                nums.len() as i32
            } else {
                indices[i + 1]
            };
            max = max.max(prefix[r as usize] - prefix[(l + 1) as usize]);
            ans.push(max);
            indices.remove(i);
        }
        ans.pop();
        ans.reverse();
        ans.push(0);
        ans
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值