Skip to content

Practice Maximum Segment Sum After Removals

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2382
LeetCode Problem

Maximum Segment Sum After Removals

Problem Statement

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.

Source: Maximum Segment Sum After Removals on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

# Example 2

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

Constraints

n == nums.length == removeQueries.length
1 <= n <= 10^5
1 <= nums[i] <= 10^9
0 <= removeQueries[i] < n
All the values of removeQueries are unique.

Related LeetCode Problems

Practice more problems similar to Maximum Segment Sum After Removals.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.