Earliest Second to Mark Indices I - Problem

You are given two 1-indexed integer arrays, nums and changeIndices, having lengths n and m, respectively.

Initially, all indices in nums are unmarked. Your task is to mark all indices in nums.

In each second s, in order from 1 to m (inclusive), you can perform one of the following operations:

  • Choose an index i in the range [1, n] and decrement nums[i] by 1.
  • If nums[changeIndices[s]] is equal to 0, mark the index changeIndices[s].
  • Do nothing.

Return an integer denoting the earliest second in the range [1, m] when all indices in nums can be marked by choosing operations optimally, or -1 if it is impossible.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,2,0], changeIndices = [2,2,2,2,3,2,2,2]
Output: 8
💡 Note: We need to decrement nums[0] twice, nums[1] twice, and mark all indices. Index 3 appears first at position 5, so we need at least 8 seconds to complete all operations.
Example 2 — Impossible Case
$ Input: nums = [1,0,1,0], changeIndices = [1,2]
Output: -1
💡 Note: changeIndices only covers indices 1 and 2, but we need to mark indices 1,2,3,4. Since indices 3 and 4 never appear, it's impossible.
Example 3 — All Zeros
$ Input: nums = [0,1,0], changeIndices = [1,2,3]
Output: 3
💡 Note: nums[1] needs 1 decrement. All indices appear exactly once, so we need 3 seconds: decrement nums[1], mark index 2, mark index 3, then we can mark index 1.

Constraints

  • 1 ≤ nums.length ≤ 5000
  • 0 ≤ nums[i] ≤ 109
  • 1 ≤ changeIndices.length ≤ 5000
  • 1 ≤ changeIndices[i] ≤ nums.length

Visualization

Tap to expand
Earliest Second to Mark Indices I INPUT nums array 2 2 0 i=1 i=2 i=3 changeIndices array 2 2 2 2 3 2 2 2 s=1 to s=8 Initial State: All indices unmarked n=3, m=8 Goal: Mark all indices as early as possible ALGORITHM STEPS 1 Binary Search Search for earliest second in range [1, m] 2 Greedy Check For each candidate time, check if marking is possible 3 Decrement Strategy Use extra seconds to decrement nums values 4 Mark When Ready Mark index when value=0 at changeIndices[s] Simulation Timeline: s1-2: dec nums[1] (2-->0) s3-4: dec nums[2] (2-->0) s5: mark i=3 (already 0) s6: mark i=2 (now 0) s7-8: dec nums[1],mark i=1 FINAL RESULT Earliest Second 8 Final State at s=8: 0 0 0 OK OK OK All indices marked! 3 of 3 complete Operations Used: 4 decrements 3 mark operations 1 do nothing Total: 8 seconds Key Insight: The greedy approach works by processing changeIndices from right to left for each candidate time. We save the last occurrence of each index for marking, using remaining seconds for decrements. Binary search finds the minimum time where total decrements needed <= available extra seconds. TutorialsPoint - Earliest Second to Mark Indices I | Greedy Approach
Asked in
Google 15 Meta 12 Amazon 8
12.4K Views
Medium Frequency
~35 min Avg. Time
445 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