C++ Linear Scan and Greedy Check
Intuition\nJust linear scan and try out at each time if it is possible to mark all\n1. Greedy property: index marking can be done at the last index mark time.\n
# Intuition\nJust linear scan and try out at each time if it is possible to mark all\n1. Greedy property: index marking can be done at the last index mark time.\n    - queue of (index mark, time) \n    - hash to keep most recent time of index mark, used to skip element in queue which doesn\'t have most rent time stamp.\n2. At event of last time of index mark, check if the index is markable at the time by maitaining how many times used for marking so far.\n\n```\nclass Solution {\npublic:\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {\n        int n = nums.size(), m = changeIndices.size();\n        if(*max_element(nums.begin(), nums.end()) > 2000)\n            return -1;\n        int sum = accumulate(nums.begin(), nums.end(), 0);\n        queue<array<int,2>> markindextime;\n        unordered_map<int, int> marktime; \n        for(int i = 0; i < m; ++i)\n        {\n            markindextime.push( {changeIndices[i]-1, i} );\n            marktime[changeIndices[i]-1] = i;\n            \n            if(marktime.size() == n && i >= sum + n - 1) {\n                int used = 0, marked = 0, failed = false;\n                for(int sz = markindextime.size(); sz; sz--)\n                {\n                    auto [idx, time] = markindextime.front();\n                    markindextime.pop();\n                    \n                    if(time == marktime[idx])\n                    {\n                        markindextime.push( {idx, time} );\n                        if(!failed) {\n                            if(auto canmark = (time - used >= nums[idx]); canmark) {\n                                used += nums[idx] + 1;\n                                marked++;\n                            }\n                            else\n                                failed = true;\n                        }\n                    }\n                }\n                if(marked == n)\n                    return i + 1;\n            }\n        }\n        return -1;\n    }\n};\n```
['C++']