Problem solution · Java

Minimum Time to Break Locks II

Minimum Time to Break Locks II: a Java solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Binary search
Source
walkccc LeetCode Solutions
Length
79 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

Binary search

For Minimum Time to Break Locks II, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 79 lines of Java from the credited upstream file 3385.java.
  • The implementation visibly relies on sequence storage.
  • 7 loop blocks detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

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 codeMinimum Time to Break Locks II · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public int findMinimumTime(int[] strength) {    int[][] costs = new int[strength.length][strength.length];    for (int turn = 1; turn <= strength.length; ++turn)      for (int j = 0; j < strength.length; j++)        costs[turn - 1][j] = (strength[j] + turn - 1) / turn;    return hungarian(costs)[costs.length - 1];  }   // Updates the currentMinimum if potentialMinimum is smaller and returns true.  private boolean updateMinimum(int[] currentMinimum, int potentialMinimum, int index) {    if (potentialMinimum < currentMinimum[index]) {      currentMinimum[index] = potentialMinimum;      return true;    }    return false;  }   // Returns an array `res` of length n (costs.length), with `res[i]` equaling  // the minimum cost to assign the first (i + 1) turns to the first (i + 1)  // locks using the Hungarian algorithm, where costs[i][j] is the energy required  // to break j-th lock in i-th turn.  //  // https://en.wikipedia.org/wiki/Hungarian_algorithm  private int[] hungarian(int[][] costs) {    final int numLocks = costs.length;    int[] res = new int[numLocks];    int[] turnPotentials = new int[numLocks];    int[] lockPotentials = new int[numLocks + 1];    int[] lockAssignments = new int[numLocks + 1];    Arrays.fill(lockAssignments, -1);     for (int currentTurn = 0; currentTurn < numLocks; ++currentTurn) {      int currentLock = numLocks;      lockAssignments[currentLock] = currentTurn;      int[] minReducedCosts = new int[numLocks + 1];      Arrays.fill(minReducedCosts, Integer.MAX_VALUE);      int[] previousLockAssignments = new int[numLocks + 1];      boolean[] locksInOptimalPath = new boolean[numLocks + 1];       while (lockAssignments[currentLock] != -1) {        locksInOptimalPath[currentLock] = true;        int assignedTurn = lockAssignments[currentLock];        int minCostDelta = Integer.MAX_VALUE;        int nextLock = -1;         for (int lock = 0; lock < numLocks; ++lock)          if (!locksInOptimalPath[lock]) {            final int reducedCost =                costs[assignedTurn][lock] - turnPotentials[assignedTurn] - lockPotentials[lock];            if (updateMinimum(minReducedCosts, reducedCost, lock))              previousLockAssignments[lock] = currentLock;            if (minReducedCosts[lock] < minCostDelta) {              minCostDelta = minReducedCosts[lock];              nextLock = lock;            }          }         for (int lock = 0; lock <= numLocks; ++lock)          if (locksInOptimalPath[lock]) {            turnPotentials[lockAssignments[lock]] += minCostDelta;            lockPotentials[lock] -= minCostDelta;          } else {            minReducedCosts[lock] -= minCostDelta;          }         currentLock = nextLock;      }       for (int lock; currentLock != numLocks; currentLock = lock)        lockAssignments[currentLock] = lockAssignments[lock = previousLockAssignments[currentLock]];       res[currentTurn] = -lockPotentials[numLocks];    }     return res;  }} 

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 ↗