Simple and Clean C++ Binary Search beats 97.99% on runtime
Intuition\n Describe your first thoughts on how to solve this problem. \nBinary Search on possible length. \n\n# Complexity\n- Time complexity: O(lgC*C)\n Add y
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBinary Search on possible length. \n\n# Complexity\n- Time complexity: O(lgC*C)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(C)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {\n\n        auto canMarked = [&](int m) {\n\n            int numDelQuota = m + 1;\n            std::vector<bool> isMarked(nums.size(), false);\n            int isMarkedNum = 0; \n            int numDelQuotaDebt = 0; // Borrow the "minus" in the future  \n            for(int i=m; i>=0; --i) {\n                // Greedy mark the nums from the end\n                if (isMarked[changeIndices[i]-1] == false) {\n                    numDelQuota -= 1 + nums[changeIndices[i]-1]; \n                    numDelQuotaDebt += nums[changeIndices[i]-1];\n                    if (numDelQuota < 0) return false; \n                    isMarked[changeIndices[i]-1] = true; \n                    ++isMarkedNum; \n                }\n                else {\n                    // The array shrink for "minus" or array shortening\n                    if (numDelQuotaDebt > 0) --numDelQuotaDebt;\n                    else --numDelQuota;\n                } \n                if (isMarkedNum == nums.size()) return true; \n            }\n            return false; \n        };\n\n        int l=0, r=changeIndices.size()-1; \n        while (l < r) {\n            int mid = (l + r) / 2;\n            bool isFinisehd = canMarked(mid);  \n            if (isFinisehd) r = mid; \n            else l = mid + 1; \n        }\n        if (canMarked(l) == true) return l + 1; \n        return -1;\n    }\n};\n```
['C++']