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
Earliest Second to Mark Indices II INPUT nums array 2 [1] 2 [2] 0 [3] changeIndices array 2 2 2 2 3 2 2 2 Seconds 1-8 (m=8) Goal: Mark all indices in nums when value reaches 0 n=3, m=8 ALGORITHM STEPS (Greedy Approach) 1 Binary Search on answer (seconds 1 to m) 2 Greedy Check Can mark all by second k? 3 Use Reset Wisely Reset saves decrement ops 4 Track Operations Count decrements needed Simulation for k=8: s=1-4: Decrement nums[1],nums[2] s=5: Mark idx 3 (already 0) s=6: Decrement remaining s=7: Mark idx 1 (now 0) s=8: Mark idx 2 (now 0) OK FINAL RESULT Final State at Second 8 0 MARKED 0 MARKED 0 MARKED Output: 8 Why 8 seconds? - Total decrements: 2+2=4 - Index 3 already 0 (mark s=5) - Need 3 mark operations - Earliest: second 8 All indices marked - OK Key Insight: The greedy approach uses binary search to find the minimum seconds. For each candidate k, we check if all indices can be marked by optimally choosing: (1) when to decrement values, (2) when to use reset operations from changeIndices to instantly zero values, and (3) when to mark indices. Reset operations save decrement steps! TutorialsPoint - Earliest Second to Mark Indices II | Greedy Approach
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