Earliest Second to Mark Indices II - Problem
Earliest Second to Mark Indices II is a challenging optimization problem that simulates a time-based marking system.

You are given two arrays: nums (1-indexed, length n) containing positive integers, and changeIndices (1-indexed, length m) containing indices that reference positions in nums.

Goal: Mark all indices in nums in the minimum number of seconds.

Rules for each second s (from 1 to m):
1. Decrement: Choose any index i and reduce nums[i] by 1
2. Reset: Set nums[changeIndices[s]] to any non-negative value
3. Mark: Mark index i if nums[i] == 0
4. Do nothing

An index can only be marked when its value reaches 0. The challenge is to optimally use reset operations (which can instantly zero out values) and decrement operations to mark everything as quickly as possible.

Return: The earliest second when all indices can be marked, or -1 if impossible.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [2, 2, 0], changeIndices = [2, 2, 2, 2, 3, 2, 2, 2]
โ€บ Output: 8
๐Ÿ’ก Note: We can mark all indices by second 8. Use reset operations on indices 2 and 3 at their last available times, then decrement and mark index 1 (which has no reset opportunity).
example_2.py โ€” Reset Required
$ Input: nums = [1, 3], changeIndices = [1, 1, 1, 2, 1, 1, 1]
โ€บ Output: 6
๐Ÿ’ก Note: Reset index 2 at second 4, mark it at second 5. For index 1, decrement once and mark, which takes 2 more operations. Total minimum time is 6 seconds.
example_3.py โ€” Impossible Case
$ Input: nums = [0, 1, 1, 2], changeIndices = [1, 1, 2]
โ€บ Output: -1
๐Ÿ’ก Note: Index 4 (with value 2) never appears in changeIndices, so we need 3 operations (2 decrements + 1 mark). But we only have 3 seconds total, and we also need to handle other indices. Impossible.

Constraints

  • 1 โ‰ค n == nums.length โ‰ค 5000
  • 0 โ‰ค nums[i] โ‰ค 109
  • 1 โ‰ค m == changeIndices.length โ‰ค 5000
  • 1 โ‰ค changeIndices[i] โ‰ค n
  • Time limit: 2000ms for optimal solution

Visualization

Tap to expand
Timeline: Seconds 1 โ†’ mDecDecResetMarkResetMarkRed: Decrement operationsGreen: Reset operationsBlue: Mark operationsGreedy Strategy: Delay resets to maximize flexibility
Understanding the Visualization
1
Identify Reset Opportunities
Find the latest time each index can be reset based on changeIndices
2
Schedule Reset Operations
Place reset+mark pairs at their latest possible times
3
Fill Remaining Time
Use remaining time slots for decrement operations
4
Verify Feasibility
Check if all operations fit within the time limit
Key Takeaway
๐ŸŽฏ Key Insight: By delaying reset operations until their latest possible moment, we maximize our scheduling flexibility and minimize the total time needed to mark all indices.
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
28.4K Views
Medium Frequency
~35 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen