The Earliest Second to Mark Indices I coding problem involves an array nums and an array changeIndices. You start at second 1. Each second, you can do one of three things: decrease a value in nums by 1, mark an index as "complete" if its value is 0 and it's currently the second specified by changeIndices, or do nothing. You want to find the earliest second at which all indices can be marked as complete.
Companies like MathWorks ask this to test a candidate's ability to combine binary search interview pattern with greedy validation. It’s a "Medium" difficulty problem that requires checking if a specific time T is sufficient to complete the task. This pattern of "finding the minimum threshold" is extremely common in technical interviews.
The problem is solved using Binary Search on the Answer.
[1, total_seconds].T, check if it's possible to mark all indices using a greedy strategy:
[1, T].nums[i] value to 0 before its last-available second.T works, try a smaller time; otherwise, try a larger time.Suppose nums = [2, 2] and changeIndices = [1, 2, 1, 2].
Check T = 4:
nums[0] to 0. Seconds 1 and 2 are available. (Possible).T = 6 (if changeIndices was longer): With more time, we can fulfill the requirements.Whenever a problem asks for the "earliest," "minimum," or "smallest" value such that a condition is met, and the condition is monotonic (if it works for T, it works for T+1), think of Binary Search on the answer.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Number of Alloys | Medium | Solve | |
| Adjacent Increasing Subarrays Detection II | Medium | Solve | |
| Capacity To Ship Packages Within D Days | Medium | Solve | |
| Cutting Ribbons | Medium | Solve | |
| Find First and Last Position of Element in Sorted Array | Medium | Solve |