Short Intuitive | Greedy | Python
python\nclass Solution:\n    def earliestSecondToMarkIndices(self, nums: List[int], changeIndices: List[int]) -> int:\n        n = len(nums)\n\n        # For ea
```python\nclass Solution:\n    def earliestSecondToMarkIndices(self, nums: List[int], changeIndices: List[int]) -> int:\n        n = len(nums)\n\n        # For each potential ending time, we\'ll try to mark indices in\n        # a greedy way, only starting from the last occurrence of \n        # each index before that ending time. We will hold off operations\n        # and keep the pending operations in can_decrement\n        for seconds in range(len(changeIndices) + 1):\n            change_indices = changeIndices[:seconds]\n\n            remaining, can_decrement = Counter(change_indices), 0\n            marked = set()\n            for i in change_indices:\n                # If this is the last occurrence of an index and it\'s \n                # not marked yet, we\'ll attempt to mark it. If we \n                # can\'t, we\'ll move to the next second and try again.\n                if remaining[i] == 1 and i not in marked:\n                    if max(0, nums[i-1] - can_decrement) == 0:\n                        marked.add(i) # We succeeded marking this index, so we add it to marked set.\n                        can_decrement -= nums[i-1]\n                    else:\n                        break\n                else:\n                    remaining[i] -= 1\n                    can_decrement += 1\n            \n            # If we have successfully marked all indices, we return \n            # the current time in seconds.\n            if len(marked) == n:\n                return seconds\n\n        return -1\n```
['Python3']