Tap here to switch tabs
Problem
Submissions

Problem: Zero Array Transformation III

med
30 min
Try to solve the Zero Array Transformation III problem.

Statement

You are given an integer array nums and a list of range queries queries, where each query queries[i] = [l_i, r_i] represents an operation that can be applied at most once.

When you apply a query [l_i, r_i], you choose an integer decrement amount for each index j in the range l_i \leq j \leq r_i, and subtract that amount from nums[j]. Each chosen decrement must be non negative, and after all applied queries, every element of nums must be exactly 00.

You may remove some queries and keep the rest. Return the maximum number of queries you can remove such that it is still possible to convert nums into a zero array using the remaining queries. If it is impossible to convert nums into a zero array even when using all queries, return −1-1.

Note: Each query provides at most 11 unit of total decrement capacity per index in its range, and you may distribute that capacity independently across indices.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 0≤0 \leq nums[i] ≤105\leq 10^5

  • 1≤1 \leq queries.length ≤105\leq 10^5

  • queries[i].length ==== 22

  • 0≤0 \leq l_i ≤\leq r_i << nums.length

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Zero Array Transformation III

med
30 min
Try to solve the Zero Array Transformation III problem.

Statement

You are given an integer array nums and a list of range queries queries, where each query queries[i] = [l_i, r_i] represents an operation that can be applied at most once.

When you apply a query [l_i, r_i], you choose an integer decrement amount for each index j in the range l_i \leq j \leq r_i, and subtract that amount from nums[j]. Each chosen decrement must be non negative, and after all applied queries, every element of nums must be exactly 00.

You may remove some queries and keep the rest. Return the maximum number of queries you can remove such that it is still possible to convert nums into a zero array using the remaining queries. If it is impossible to convert nums into a zero array even when using all queries, return −1-1.

Note: Each query provides at most 11 unit of total decrement capacity per index in its range, and you may distribute that capacity independently across indices.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 0≤0 \leq nums[i] ≤105\leq 10^5

  • 1≤1 \leq queries.length ≤105\leq 10^5

  • queries[i].length ==== 22

  • 0≤0 \leq l_i ≤\leq r_i << nums.length