๐ฏ Zero Array Transformation IV
You're given an integer array nums and a collection of transformation queries. Each query allows you to select any subset of indices within a specified range and decrement their values by a given amount.
Your mission: Find the minimum number of queries needed to transform the entire array into zeros! ๐
Input:
nums: An array of positive integersqueries: Each query[li, ri, vali]lets you:- Pick any indices between
liandri(inclusive) - Subtract exactly
valifrom each selected index
Output: Return the minimum k such that after applying the first k queries in order, all elements become 0. If impossible, return -1.
Example: With nums = [2, 1, 3] and queries that can subtract 1 from different ranges, you need to strategically apply queries to zero out all elements efficiently!
Input & Output
Visualization
Time & Space Complexity
log q binary search iterations, each simulating q queries on n elements
Temporary arrays for simulation and difference array
Constraints
- 1 โค nums.length โค 105
- 0 โค nums[i] โค 105
- 1 โค queries.length โค 105
- queries[i].length = 3
- 0 โค li โค ri < nums.length
- 1 โค vali โค 105