3356: Zero-Array-Transformation-II
Medium

Using a difference_array, it is possible to get an O(n) solution to this problem.

Code:

class Solution {
public:
    int minZeroArray(vector<int>& nums, vector<vector<int>>& queries) {
        // this will equate to the sum of all the queries at a `nums_index`
        int current_difference = 0;

        // this is the query_index and the nums_index
        int query_index = 0, nums_index = 0;

        // the holy grail; difference_array
        vector<int> difference_array(nums.size()+1, 0);

        for (; nums_index < nums.size();) {
            // at every nums_index, we will update `current_difference` with all the current queries
            current_difference += difference_array[nums_index];
            if (current_difference >= nums[nums_index]) {
                // if the `current_difference` is >= the value at `nums_index`,
                // then increment nums_index

                // NOTE: can take this greedy approach since we know that
                // if a `nums_index` has been satisfied, then it will 
                // continue to be satisfied even after we increment
                // `query_index`
                ++nums_index;
            } else {
                for (; query_index < queries.size() && current_difference < nums[nums_index]; ++query_index) {
                    // difference_array just tracks the initial increment and decrement of each query
                    // e.g. for difference_array [0, 0, 0, 0]
                    // a query like [0, 2, 1]
                    // would update it to [1, 0, 0, -1]
                        // to show that we would increment `current_difference`
                        // by 1 at index `0`, it would stay 1 for indices `0-2`
                        // until we decrement `current_difference` at index `3`
                    // e.g. consequently for [1, 0, 0, -1]
                    // a query like [0, 1, 3]
                    // would update it to [4, 0, -3, -1]
                    difference_array[queries[query_index][0]] += queries[query_index][2];
                    difference_array[queries[query_index][1]+1] -= queries[query_index][2];
                    
                    // then, if the current_index is inside a query, we have to update it directly
                    // (since it is already >= queries[query_index][0], it will miss the increment)
                    if (queries[query_index][0] <= nums_index && nums_index <= queries[query_index][1]) {
                        current_difference += queries[query_index][2];
                    }
                }
                // finally, if we break the `for` loop and the inequality is not satisfied,
                // it implies that processing all the queries does not satisfy the `nums` array
                if (current_difference >= nums[nums_index]) ++nums_index;
                else return -1;
            }
        }
        return query_index;
    }
};

Complexity:

Learnings: