Advertisement

Earliest Second to Mark Indices I - LeetCode 3048 Solution

Earliest Second to Mark Indices I - Complete Solution Guide

Earliest Second to Mark Indices I is LeetCode problem 3048, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 sec

Detailed Explanation

The problem asks us to find the earliest second `s` in the `changeIndices` array such that all indices in the `nums` array can be marked. We are given `nums` (representing the initial values at each index) and `changeIndices` (representing the index that can be marked at each second if its corresponding value in `nums` is 0). In each second, we can either decrement a value in `nums`, mark an index if its corresponding value in `nums` is 0 and it appears in `changeIndices` at that second, or do nothing. The goal is to find the smallest `s` such that all indices in `nums` are marked by second `s`. If it is not possible to mark all indices, return -1.

Solution Approach

The solution uses binary search to find the earliest second. The `check` function simulates the marking process for a given number of seconds. It first finds the last occurrence of each index in `changeIndices` within the range [0, k-1]. If any index is missing, it's impossible to mark all indices, so the function returns `false`. Then, it iterates through the first `k` seconds. If the current second is the last time an index appears, it *must* be marked at that time. Before marking, we need to ensure enough decrement operations have been performed to reduce the corresponding `nums` value to 0. If not, return `false`. Otherwise, use decrement operations to reduce the value. Seconds where the current index is *not* the last occurrence can be used for decrement operations.

Step-by-Step Algorithm

  1. Step 1: Initialize `low` to 1 and `high` to `m` (the length of `changeIndices`). Initialize `ans` to -1.
  2. Step 2: Perform binary search between `low` and `high`.
  3. Step 3: In each iteration of the binary search, calculate `mid` as the middle point.
  4. Step 4: Call the `check` function with `mid`. If `check(mid)` returns `true`, update `ans` to `mid` and set `high` to `mid - 1` to search for an earlier second. Otherwise, set `low` to `mid + 1`.
  5. Step 5: The `check` function does the following:
  6. - Create a `last_pos` map to store the last occurrence of each index in `changeIndices` within the first `k` seconds.
  7. - If the number of unique indices found is not equal to `n` (the length of `nums`), return `false` (not all indices can be marked).
  8. - Initialize `free_slots` to 0.
  9. - Iterate through the first `k` seconds. For each second `s`:
  10. - If `s` is the last occurrence of `changeIndices[s]` in the first `k` seconds:
  11. - Calculate `decrements_needed` as `nums[changeIndices[s] - 1]`.
  12. - If `free_slots` is less than `decrements_needed`, return `false` (not enough decrement operations available).
  13. - Subtract `decrements_needed` from `free_slots`.
  14. - Otherwise, increment `free_slots` by 1 (use this second for a decrement operation).
  15. - If the loop completes without returning `false`, return `true` (all indices can be marked).
  16. Step 6: After the binary search completes, return `ans`.

Key Insights

  • Insight 1: The problem can be solved using binary search because we are looking for the *earliest* second. If it's possible to mark all indices by second `k`, then it's also possible by any second greater than `k` (though not necessarily optimally).
  • Insight 2: A crucial step is devising a `check` function that determines if it's possible to mark all indices within a given number of seconds `k`. This function simulates the process of decrementing and marking, trying to optimize the operations.
  • Insight 3: We need to keep track of the *last* time each index appears in `changeIndices` within the considered time frame (first `k` seconds). This is important because that second is when that index *must* be marked if possible.

Complexity Analysis

Time Complexity: O(m * (n + m))

Space Complexity: O(n)

Topics

This problem involves: Array, Binary Search.

Companies

Asked at: MathWorks.