C++ | Beats 100% | Priority_queue + Sweep Line
Intuition\nI was unable to solve the problem during contest but just after the contest i was able to do it felt bad. for every index we need atleast nums[index]
# Intuition\nI was unable to solve the problem during contest but just after the contest i was able to do it felt bad. for every index we need atleast nums[index] queries to make it 0. sort the queries processing from first index will make life easy. Push the queries in your queue as long as they contain that index in the range and sort them with furthermost index so if we process any query it can effect maximum ranges, only process till your current value becomes zero, now sweeping the line for whole array will result in counting some values multiple times so only process the values for current index, do this before starting to push your values in max heap so that if the value already became zero you do not need to process it the size of the max heap is the unprocessed queries\n\n# Approach\nuse priority_queue sort the ranges for each index as you process based on max length and then pop the values subtracting the 1 from each range\n\n# Complexity\n- Time complexity:\nO(NlogN)\n\n- Space complexity:\nO(N)\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {\n\n      ios::sync_with_stdio(false);\n      cin.tie(0); cout.tie(0);\n      priority_queue<pair<int,int>> pq;\n      int n = queries.size();\n      sort(queries.begin(), queries.end());\n      vector<int> v(nums.size() + 1, 0);\n      int i = 0, k = 0;\n      while (k < nums.size()) {\n        if (k > 0)\n          v[k] += v[k - 1];\n        while (i < n && queries[i][0] <= k)\n            pq.push({queries[i][1], queries[i][0]}), i++;\n        while (k < nums.size() && !pq.empty() && nums[k] + v[k] > 0 && pq.top().first >= k) {\n          v[k] -= 1;\n          v[pq.top().first + 1] += 1;\n          pq.pop();\n        }\n        if (nums[k] + v[k] > 0)\n          return -1;\n        \n        k++;\n      }\n\n      return pq.size();\n    }\n};\n```
['C++']