Earliest Second to Mark Indices I

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.

Example 1
Inputnums = [2,2,0], changeIndices = [2,2,2,2,3,2,2,1]
Output8
By decrementing indices 1 and 2 to zero before their mark opportunities and marking index 3 at second 5, index 2 at second 6, and index 1 at second 8, all indices are marked at the earliest possible second 8.
Example 2
Inputnums = [1,3], changeIndices = [1,1,1,2,1,1,1]
Output6
After decrementing index 2 during the first three seconds, marking it at second 4, then decrementing index 1 and marking it at second 6, all indices are marked at the earliest possible second 6.

Constraints

  • 1 <= n == nums.length <= 2000
  • 0 <= nums[i] <= 10^9
  • 1 <= m == changeIndices.length <= 2000
  • 1 <= changeIndices[i] <= n

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate