Problem solution · Java

Earliest Second to Mark Indices II

Earliest Second to Mark Indices II: a Java solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Breadth-first search
Source
walkccc LeetCode Solutions
Length
81 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For Earliest Second to Mark Indices II, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 81 lines of Java from the credited upstream file 3049.java.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup, work queue.
  • 5 loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeEarliest Second to Mark Indices II · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {    final long numsSum = Arrays.stream(nums).asLongStream().sum();    // {the second: the index of nums can be zeroed at the current second}    Map<Integer, Integer> secondToIndex = getSecondToIndex(nums, changeIndices);    int l = 0;    int r = changeIndices.length + 1;     while (l < r) {      final int m = (l + r) / 2;      if (canMark(nums, secondToIndex, m))        r = m;      else        l = m + 1;    }     return l <= changeIndices.length ? l : -1;  }   // Returns true if all indices of `nums` can be marked within `maxSecond`.  private boolean canMark(int[] nums, Map<Integer, Integer> secondToIndex, int maxSecond,                          final long numsSum) {    // Use a min-heap to greedily pop out the minimum number, which yields the    // least saving.    Queue<Integer> minHeap = new PriorityQueue<>();    int marks = 0;     for (int second = maxSecond - 1; second >= 0; --second) {      if (secondToIndex.containsKey(second)) {        // The number mapped by the index is a candidate to be zeroed out.        final int index = secondToIndex.get(second);        minHeap.offer(nums[index]);        if (marks == 0) {          // Running out of marks, so need to pop out the minimum number.          // So, the current second will be used to mark an index.          minHeap.poll();          ++marks;        } else {          // There're enough marks.          // So, the current second will be used to zero out a number.          --marks;        }      } else {        // There's no candidate to be zeroed out.        // So, the current second will be used to mark an index.        ++marks;      }    }     final int heapSize = minHeap.size();    final long decrementAndMarkCost = numsSum - getHeapSum(minHeap) + (nums.length - heapSize);    final long zeroAndMarkCost = heapSize + heapSize;    return decrementAndMarkCost + zeroAndMarkCost <= maxSecond;  }   private long getHeapSum(Queue<Integer> minHeap) {    long sum = 0;    while (!minHeap.isEmpty())      sum += minHeap.poll();    return sum;  }   private Map<Integer, Integer> getSecondToIndex(int[] nums, int[] changeIndices) {    // {the `index` of nums: the earliest second to zero out nums[index]}    Map<Integer, Integer> indexToFirstSecond = new HashMap<>();    Map<Integer, Integer> secondToIndex = new HashMap<>();    for (int zeroIndexedSecond = 0; zeroIndexedSecond < changeIndices.length; ++zeroIndexedSecond) {      // Convert to 0-indexed.      final int index = changeIndices[zeroIndexedSecond] - 1;      if (nums[index] > 0)        indexToFirstSecond.putIfAbsent(index, zeroIndexedSecond);    }    for (Map.Entry<Integer, Integer> entry : indexToFirstSecond.entrySet()) {      final int index = entry.getKey();      final int second = entry.getValue();      secondToIndex.put(second, index);    }    return secondToIndex;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗