- Identify the ordered answer range or sorted search domain.
- Write a predicate whose truth changes only once.
- Move the appropriate boundary after each midpoint check and return the final feasible position.
Code notes
- 84 lines of C++ from the credited upstream file 3385.cpp.
- 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.
Use this to learn the idea, then write your own version.
1class Solution {2 public:3 int findMinimumTime(vector<int>& strength) {4 vector<vector<int>> costs;5 6 for (int turn = 1; turn <= strength.size(); ++turn) {7 vector<int> cost;8 for (const int s : strength)9 cost.push_back((s + turn - 1) / turn);10 costs.push_back(cost);11 }12 13 return hungarian(costs).back();14 }15 16 private:17 18 bool updateMinimum(int& currentMinimum, const int& potentialMinimum) {19 if (potentialMinimum < currentMinimum) {20 currentMinimum = potentialMinimum;21 return true;22 }23 return false;24 }25 26 27 28 29 30 31 32 vector<int> hungarian(const vector<vector<int>>& costs) {33 const int numLocks = costs.size();34 vector<int> res;35 vector<int> lockAssignments(numLocks + 1, -1);36 vector<int> turnPotentials(numLocks);37 vector<int> lockPotentials(numLocks + 1);38 39 for (int currentTurn = 0; currentTurn < numLocks; ++currentTurn) {40 int currentLock = numLocks;41 lockAssignments[currentLock] = currentTurn;42 vector<int> minReducedCosts(numLocks + 1, INT_MAX);43 vector<int> previousLockAssignments(numLocks + 1, -1);44 vector<bool> locksInOptimalPath(numLocks + 1);45 46 while (lockAssignments[currentLock] != -1) {47 locksInOptimalPath[currentLock] = true;48 const int assignedTurn = lockAssignments[currentLock];49 int minCostDelta = INT_MAX;50 int nextLock;51 52 for (int lock = 0; lock < numLocks; ++lock)53 if (!locksInOptimalPath[lock]) {54 const int reducedCost = costs[assignedTurn][lock] -55 turnPotentials[assignedTurn] -56 lockPotentials[lock];57 if (updateMinimum(minReducedCosts[lock], reducedCost))58 previousLockAssignments[lock] = currentLock;59 if (updateMinimum(minCostDelta, minReducedCosts[lock]))60 nextLock = lock;61 }62 63 for (int lock = 0; lock <= numLocks; ++lock)64 if (locksInOptimalPath[lock]) {65 turnPotentials[lockAssignments[lock]] += minCostDelta;66 lockPotentials[lock] -= minCostDelta;67 } else {68 minReducedCosts[lock] -= minCostDelta;69 }70 71 currentLock = nextLock;72 }73 74 for (int lock; currentLock != numLocks; currentLock = lock)75 lockAssignments[currentLock] =76 lockAssignments[lock = previousLockAssignments[currentLock]];77 78 res.push_back(-lockPotentials[numLocks]);79 }80 81 return res;82 }83};84