Priority Queue | C++
\n# Approach\nFor every i from 1 to m, store the last occurence of the index in changeIndices with number of elements needed to mark.\n\n# Complexity\n- Time co
\n# Approach\nFor every i from 1 to m, store the last occurence of the index in changeIndices with number of elements needed to mark.\n\n# Complexity\n- Time complexity: O(m * nlog(n))\n\n# Code\n```\nclass Solution {\npublic:\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& ci) {\n        int n = nums.size(), m = ci.size();\n        int ans = -1;\n        for (int i = 0; i < m; ++i)\n        {\n            vector<int> last_occ(n+1, -1);\n            for(int j = 0 ; j <= i ; j++){\n                last_occ[ci[j]] = j+1;\n            }\n            priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq; // last occurence of the index in changeIndices, number of elements needed to mark\n            int flag = 0;\n            for (int j = 1; j <= n; ++j)\n            {\n                if(last_occ[j] == -1){\n                    flag = 1;\n                    break;\n                }\n                pq.push({last_occ[j], nums[j-1]+1});\n            }\n            if(flag) continue;\n            flag = 0;\n            int rem = 0;\n            while(!pq.empty()){\n                int need = pq.top().second;\n                int ind = pq.top().first;\n                pq.pop();\n                if(ind-rem >= need){\n                    rem += need;\n                }\n                else{\n                    flag = 1;\n                    break;\n                }\n            }\n            if(flag == 0 && pq.size() == 0){\n                ans = i+1;\n                break;\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']