3048. Earliest Second to Mark Indices I LeetCode Solution

In this guide, you will get 3048. Earliest Second to Mark Indices I LeetCode Solution with the best time and space complexity. The solution to Earliest Second to Mark Indices I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Earliest Second to Mark Indices I solution in C++
  4. Earliest Second to Mark Indices I solution in Java
  5. Earliest Second to Mark Indices I solution in Python
  6. Additional Resources
3048. Earliest Second to Mark Indices I LeetCode Solution image

Problem Statement of 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:

Input: nums = [2,2,0], changeIndices = [2,2,2,2,3,2,2,1]
Output: 8
Explanation: In this example, we have 8 seconds. The following operations can be performed to mark all indices:
Second 1: Choose index 1 and decrement nums[1] by one. nums becomes [1,2,0].
Second 2: Choose index 1 and decrement nums[1] by one. nums becomes [0,2,0].
Second 3: Choose index 2 and decrement nums[2] by one. nums becomes [0,1,0].
Second 4: Choose index 2 and decrement nums[2] by one. nums becomes [0,0,0].
Second 5: Mark the index changeIndices[5], which is marking index 3, since nums[3] is equal to 0.
Second 6: Mark the index changeIndices[6], which is marking index 2, since nums[2] is equal to 0.
Second 7: Do nothing.
Second 8: Mark the index changeIndices[8], which is marking index 1, since nums[1] is equal to 0.
Now all indices have been marked.
It can be shown that it is not possible to mark all indices earlier than the 8th second.
Hence, the answer is 8.

Example 2:

Input: nums = [1,3], changeIndices = [1,1,1,2,1,1,1]
Output: 6
Explanation: In this example, we have 7 seconds. The following operations can be performed to mark all indices:
Second 1: Choose index 2 and decrement nums[2] by one. nums becomes [1,2].
Second 2: Choose index 2 and decrement nums[2] by one. nums becomes [1,1].
Second 3: Choose index 2 and decrement nums[2] by one. nums becomes [1,0].
Second 4: Mark the index changeIndices[4], which is marking index 2, since nums[2] is equal to 0.
Second 5: Choose index 1 and decrement nums[1] by one. nums becomes [0,0].
Second 6: Mark the index changeIndices[6], which is marking index 1, since nums[1] is equal to 0.
Now all indices have been marked.
It can be shown that it is not possible to mark all indices earlier than the 6th second.
Hence, the answer is 6.

Example 3:

Input: nums = [0,1], changeIndices = [2,2,2]
Output: -1
Explanation: In this example, it is impossible to mark all indices because index 1 isn’t in changeIndices.
Hence, the answer is -1.

Constraints:

1 <= n == nums.length <= 2000
0 <= nums[i] <= 109
1 <= m == changeIndices.length <= 2000
1 <= changeIndices[i] <= n

Complexity Analysis

  • Time Complexity: O(m\log m)
  • Space Complexity: O(m)

3048. Earliest Second to Mark Indices I LeetCode Solution in C++

class Solution {
 public:
  int earliestSecondToMarkIndices(vector<int>& nums,
                                  vector<int>& changeIndices) {
    int l = 0;
    int r = changeIndices.size() + 1;

    while (l < r) {
      const int m = (l + r) / 2;
      if (canMark(nums, changeIndices, m))
        r = m;
      else
        l = m + 1;
    }

    return l <= changeIndices.size() ? l : -1;
  }

 private:
  // Returns true if all indices of `nums` can be marked within `second`.
  bool canMark(const vector<int>& nums, const vector<int>& changeIndices,
               int second) {
    int numMarked = 0;
    int decrement = 0;
    // indexToLastSecond[i] := the last second to mark the index i
    vector<int> indexToLastSecond(nums.size(), -1);

    for (int i = 0; i < second; ++i)
      indexToLastSecond[changeIndices[i] - 1] = i;

    for (int i = 0; i < second; ++i) {
      const int index = changeIndices[i] - 1;  // Convert to 0-indexed.
      if (i == indexToLastSecond[index]) {
        // Reach the last occurrence of the number.
        // So, the current second will be used to mark the index.
        if (nums[index] > decrement)
          // The decrement is less than the number to be marked.
          return false;
        decrement -= nums[index];
        ++numMarked;
      } else {
        ++decrement;
      }
    }

    return numMarked == nums.size();
  }
};
/* code provided by PROGIEZ */

3048. Earliest Second to Mark Indices I LeetCode Solution in Java

class Solution {
  public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {
    int l = 0;
    int r = changeIndices.length + 1;

    while (l < r) {
      final int m = (l + r) / 2;
      if (canMark(nums, changeIndices, m))
        r = m;
      else
        l = m + 1;
    }

    return l <= changeIndices.length ? l : -1;
  }

  // Returns true if all indices of `nums` can be marked within `second`.
  private boolean canMark(int[] nums, int[] changeIndices, int second) {
    int numMarked = 0;
    int decrement = 0;
    // indexToLastSecond[i] := the last second to mark the index i
    int[] indexToLastSecond = new int[nums.length];
    Arrays.fill(indexToLastSecond, -1);

    for (int i = 0; i < second; ++i)
      indexToLastSecond[changeIndices[i] - 1] = i;

    for (int i = 0; i < second; ++i) {
      final int index = changeIndices[i] - 1; // Convert to 0-indexed.
      if (i == indexToLastSecond[index]) {
        // Reach the last occurrence of the number.
        // So, the current second will be used to mark the index.
        if (nums[index] > decrement)
          // The decrement is less than the number to be marked.
          return false;
        decrement -= nums[index];
        ++numMarked;
      } else {
        ++decrement;
      }
    }

    return numMarked == nums.length;
  }
}
// code provided by PROGIEZ

3048. Earliest Second to Mark Indices I LeetCode Solution in Python

class Solution:
  def earliestSecondToMarkIndices(
      self,
      nums: list[int],
      changeIndices: list[int],
  ) -> int:
    def canMark(second: int) -> bool:
      """
      Returns True if all indices of `nums` can be marked within `second`.
      """
      numMarked = 0
      decrement = 0
      indexToLastSecond = {}

      for i in range(second):
        indexToLastSecond[changeIndices[i] - 1] = i

      for i in range(second):
        index = changeIndices[i] - 1  # Convert to 0-indexed
        if i == indexToLastSecond[index]:
          # Reach the last occurrence of the number.
          # So, the current second will be used to mark the index.
          if nums[index] > decrement:
            # The decrement is less than the number to be marked.
            return False
          decrement -= nums[index]
          numMarked += 1
        else:
          decrement += 1

      return numMarked == len(nums)

    l = 1
    r = len(changeIndices) + 1
    ans = bisect.bisect_left(range(l, r), True, key=canMark) + l
    return ans if ans <= len(changeIndices) else -1
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.