Maximum Segment Sum After Removals

You are given two 0-indexed integer arrays nums and removeQueries, both of length n. For the i^th 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 i^th removal.

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

Example 1
Inputnums = [1,2,5,6,1], removeQueries = [0,3,2,4,1]
Output[14,7,2,2,0]
After the removals, the maximum segment sums are 14, 7, 2, 2, and 0 respectively.
Example 2
Inputnums = [3,2,11,1], removeQueries = [3,2,1,0]
Output[16,5,3,0]
After the removals, the maximum segment sums are 16, 5, 3, and 0 respectively.

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.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate