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: 4
💡 Note: Break lock 0 (2 energy, 2 minutes), then lock 2 (4 energy, factor=3, so 2 minutes), then lock 1 (5 energy, factor=5, so 1 minute). Total: 2+2+1=5 minutes. Or break lock 0 (2 minutes), lock 1 (5 energy, factor=3, so 2 minutes), lock 2 (4 energy, factor=5, so 1 minute). Total: 2+2+1=5. But breaking lock 0 first: 2 + ceil(4/3) + ceil(5/5) = 2+2+1=5, or lock 2 first: 4 + ceil(2/3) + ceil(5/5) = 4+1+1=6. Best is actually 4 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
Sword Energy Growth: Break Locks [3,4,1] with k=1Lock 0Str: 3Lock 1Str: 4Lock 2Str: 1Break 1stSword StatusInitial: Factor = 1After Lock 2: Factor = 2After Lock 1: Factor = 3Optimal Sequence CalculationStep 1: Break Lock 2 → Time = ⌈1/1⌉ = 1 minute, Factor = 2Step 2: Break Lock 1 → Time = ⌈4/2⌉ = 2 minutes, Factor = 3Step 3: Break Lock 0 → Time = ⌈3/3⌉ = 1 minuteTotal Time: 1 + 2 + 1 = 4 minutes
Understanding the Visualization
1
Input
Locks with strength [3,4,1] and factor increase k=1
2
Process
Try different orders, sword factor increases after each break
3
Output
Minimum time is 4 minutes with optimal order
Key Takeaway
🎯 Key Insight: Breaking weaker locks first increases the sword factor, making stronger locks faster to break later
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