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 i-th 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 i-th 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 a given value k

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], k = 1
Output: 4
💡 Note: Optimal order: Break lock 2 (1 energy, 1 minute), then lock 1 (4 energy, factor=2, so 2 minutes), then lock 0 (3 energy, factor=3, so 1 minute). Total: 1+2+1=4 minutes.
Example 2 — Higher k Value
$ Input: strength = [2,5,4], k = 2
Output: 5
💡 Note: Break lock 0 (2 energy, 2 minutes), then lock 2 (4 energy, factor=3, so ceil(4/3)=2 minutes), then lock 1 (5 energy, factor=5, so ceil(5/5)=1 minute). Total: 2+2+1=5 minutes.
Example 3 — Single Lock
$ Input: strength = [10], k = 3
Output: 10
💡 Note: Only one lock with strength 10, initial factor is 1, so it takes 10 minutes to break.

Constraints

  • 1 ≤ n ≤ 8
  • 1 ≤ strength[i] ≤ 108
  • 1 ≤ k ≤ 108

Visualization

Tap to expand
Minimum Time to Break Locks I INPUT Dungeon with 3 Locks Lock 0 str=3 Lock 1 str=4 Lock 2 str=1 strength = [3, 4, 1] k = 1 Sword Properties: Initial energy: 0 Initial factor x: 1 Energy grows by x/min After break: x += k Energy resets to 0 ALGORITHM STEPS 1 Try All Orders Permute lock breaking order 2 Calculate Time time = ceil(str[i] / x) 3 Update Factor x = x + k after each lock 4 Find Minimum Track min total time Optimal: [1, 3, 4] (indices 2,0,1) Lock 2(str=1): x=1 time=ceil(1/1)=1 min Lock 0(str=3): x=2 time=ceil(3/2)=2 min Lock 1(str=4): x=3 time=ceil(4/3)=2 min FINAL RESULT Timeline (4 minutes total) Min 1 Min 2 Min 3 Min 4 Break Lock 2 Break Lock 0 Break Lock 1 All Locks Broken! OK OK OK OUTPUT 4 Minimum time = 4 minutes Bob escapes the dungeon! Order: [1,3,4] = 1+2+1 = 4 Key Insight: Since x increases after each lock, break weaker locks FIRST with low x, save stronger locks for when x is higher. With n! permutations (small n), try all orders. Time for lock i = ceil(strength[i] / current_x). Optimal order [1,3,4]: x=1 breaks 1 in 1min, x=2 breaks 3 in 2min, x=3 breaks 4 in 2min --> Total = 4 min TutorialsPoint - Minimum Time to Break Locks I | Optimal Solution
Asked in
Google 15 Amazon 12 Microsoft 8
23.0K 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