With intution and approach 2 heap solution with explaination and algo
# Intuition\n\nTransforming nums into a zero array involves applying the queries efficiently. The challenge lies in determining the maximum number of queries th
### **# Intuition**\n\nTransforming `nums` into a zero array involves applying the queries efficiently. The challenge lies in determining the maximum number of queries that can be excluded without preventing the transformation of `nums` to a zero array. Using a **max-heap** to prioritize queries with the largest range and a **min-heap** to track active ranges helps achieve this goal.\n\n---\n\n### **# Approach**\n\n1. **Sort the Queries**:\n   - Sort the queries by their starting index (`li`) so that they can be processed sequentially with respect to the `nums` array.\n\n2. **Use Two Heaps**:\n   - **Max-Heap** (`maxHeap`): Stores the ending indices (`ri`) of the queries that are currently being considered. The top element of the heap always represents the query covering the largest range.\n   - **Min-Heap** (`minHeap`): Tracks active queries for the current index `i`. It helps decrement the value of `nums[i]` effectively.\n\n3. **Process `nums` Array**:\n   - For each index `i` in `nums`, perform the following:\n     - Push the `ri` of queries starting at or before `i` into the max-heap.\n     - Remove expired ranges (queries ending before `i`) from the min-heap.\n     - Reduce `nums[i]` by the number of active queries (`minHeap.size()`).\n     - If `nums[i] > 0`, move additional queries from the max-heap to the min-heap to decrement `nums[i]`. If `nums[i]` cannot be reduced to zero, return `-1`.\n\n4. **Result**:\n   - After processing the entire array, the size of the max-heap represents the number of unused queries, as they were not required to transform `nums` into a zero array.\n\n\n---\n\n### **# Complexity**\n\n- **Time Complexity**:\n  - Sorting queries: \\(O(q \\log q)\\), where \\(q\\) is the number of queries.\n  - Processing each element in `nums`:\n    - Each query is pushed and popped from the heaps at most once: \\(O(q \\log q)\\).\n    - Each element in `nums` is processed once: \\(O(n)\\).\n  - Overall: \\(O((n + q) \\log q)\\).\n\n- **Space Complexity**:\n  - Max-heap and min-heap: \\(O(q)\\).\n  - Sorting storage: \\(O(q)\\).\n  - Overall: \\(O(q)\\).\n\n---\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {\n\n        sort(queries.begin(),queries.end());\n\n        int n = nums.size();\n        int nq = queries.size();\n\n        priority_queue<int>maxHeap;\n        priority_queue<int,vector<int>,greater<int>>minHeap;\n\n        for(int i = 0,j = 0; i < n; i++){\n\n            while(j < nq && queries[j][0] <= i){\n                maxHeap.push(queries[j][1]);\n                j++;\n            }\n\n            while(!minHeap.empty() && minHeap.top() < i){\n                minHeap.pop();\n            }\n\n            nums[i] -= minHeap.size();\n\n            if(nums[i] <= 0)continue;\n\n            while( !maxHeap.empty() && maxHeap.top() >= i && nums[i] > 0 ){\n                minHeap.push(maxHeap.top());\n                maxHeap.pop();\n                nums[i]--;\n            }\n            \n            if(nums[i] > 0){\n                return -1;\n            }\n        }\n\n        return maxHeap.size();        \n    }\n};\n```\n\nYour upvote motivates me to write more solutions to community.\n\n
['C++']