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
iin the range[1, n]and decrementnums[i]by1. - If
nums[changeIndices[s]]is equal to0, mark the indexchangeIndices[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
Input
nums = [2,2,0], changeIndices = [2,2,2,2,3,2,2,1]Output
8By 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
Input
nums = [1,3], changeIndices = [1,1,1,2,1,1,1]Output
6After 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