3048. Earliest Second to Mark Indices I
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```swift []
class Solution {
    func earliestSecondToMarkIndices(_ nums: [Int], _ changeIndices: [Int]) -> Int {
        let totalNums = nums.count
        let totalChanges = changeIndices.count
        
        let requiredMarks = nums.reduce(0, +) + totalNums
        
        if requiredMarks > totalChanges {
            return -1
        }
        
        for currentTime in requiredMarks...totalChanges {
            var processedIndices = Set<Int>()
            var remainingMarks = requiredMarks
            var isValid = true
            
            for step in (0..<currentTime).reversed() {
                if remainingMarks > step + 1 {
                    isValid = false
                    break
                }
                
                let changeIndex = changeIndices[step]
                
                if !processedIndices.contains(changeIndex) {
                    processedIndices.insert(changeIndex)
                    remainingMarks -= (nums[changeIndex - 1] + 1)
                }
            }
            
            if isValid && remainingMarks == 0 {
                return currentTime
            }
        }
        
        return -1
    }
}

```
['Swift']