Minimum Time to Break Locks II - Problem

Bob is stuck in a dungeon and must break n locks, each requiring some amount of energy to break. The required energy for each lock is stored in an array called strength where strength[i] indicates the energy needed to break the ith lock.

To break a lock, Bob uses a sword with the following characteristics:

  • The initial energy of the sword is 0
  • The initial factor X by which the energy of the sword increases is 1
  • Every minute, the energy of the sword increases by the current factor X
  • To break the ith lock, the energy of the sword must reach at least strength[i]
  • After breaking a lock, the energy of the sword resets to 0, and the factor X increases by 1

Your task is to determine the minimum time in minutes required for Bob to break all n locks and escape the dungeon.

Input & Output

Example 1 — Basic Case
$ Input: strength = [3, 4, 1]
Output: 5
💡 Note: Optimal order is [1, 3, 4]: Factor 1 breaks lock with strength 1 in 1 minute, factor 2 breaks lock with strength 3 in 2 minutes (ceil(3/2)), factor 3 breaks lock with strength 4 in 2 minutes (ceil(4/3)). Total: 1 + 2 + 2 = 5 minutes.
Example 2 — Equal Strengths
$ Input: strength = [2, 2, 2]
Output: 4
💡 Note: All locks have equal strength, so order doesn't matter: Factor 1 takes ceil(2/1)=2 minutes, factor 2 takes ceil(2/2)=1 minute, factor 3 takes ceil(2/3)=1 minute. Total: 2 + 1 + 1 = 4 minutes.
Example 3 — Single Lock
$ Input: strength = [5]
Output: 5
💡 Note: Only one lock with strength 5. Factor starts at 1, so it takes ceil(5/1) = 5 minutes to break.

Constraints

  • 1 ≤ strength.length ≤ 8
  • 1 ≤ strength[i] ≤ 106

Visualization

Tap to expand
Minimum Time to Break Locks II INPUT Lock 0 str=3 Lock 1 str=4 Lock 2 str=1 strength array: [3, 4, 1] Sword Properties Initial Energy: 0 Initial Factor X: 1 X increases by 1 after break ALGORITHM STEPS 1 Optimal Order Find best order to break locks (Hungarian algo) 2 Break Lock 2 (str=1) X=1, need 1 energy Time: ceil(1/1) = 1 min 3 Break Lock 0 (str=3) X=2, need 3 energy Time: ceil(3/2) = 2 min 4 Break Lock 1 (str=4) X=3, need 4 energy Time: ceil(4/3) = 2 min Time Calculation Order: [2, 0, 1] Time: 1 + 2 + 2 = 5 FINAL RESULT Timeline 1 min 2 min 2 min Lock 2 Lock 0 Lock 1 Breaking Sequence Min 0-1: Break Lock 2 (X=1) Min 1-3: Break Lock 0 (X=2) Min 3-5: Break Lock 1 (X=3) OK - All locks broken! Output 5 minutes Key Insight: The Hungarian Algorithm finds optimal assignment of locks to breaking order. Time to break lock with strength S when factor is X: ceil(S/X) minutes. Breaking weaker locks first with lower X, stronger locks later with higher X minimizes total time. TutorialsPoint - Minimum Time to Break Locks II | Optimal Solution (Hungarian Algorithm)
Asked in
Amazon 15 Google 12 Microsoft 8
29.4K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen