681. Minimum Time to Break Locks II
Bob is trapped in a dungeon with n locks. Each lock requires a certain amount of energy, specified in an array strength (where strength[i] is the energy needed for the ith lock). Bob’s sword starts with energy 0 and a multiplier X = 1. Each minute, the sword’s energy increases by X. To break a lock, the energy must be at least the lock’s required energy. After breaking a lock, the sword’s energy resets to 0 and X increases by 1.
Determine the minimum number of minutes required for Bob to break all n locks.
Example 1:
Input: strength = [2, 3, 1]
Output: 3
Explanation:
- Minute 0: Energy = 0
- Minute 1: Energy = 1 → Break lock requiring 1 energy (X becomes 2)
- Minute 2: Energy = 2 → Break lock requiring 2 energy (X becomes 3)
- Minute 3: Energy = 3 → Break lock requiring 3 energy
- Total time: 3 minutes
Example 2:
Input: strength = [4, 6, 2]
Output: 6
Explanation:
- Minute 0: Energy = 0
- Minute 1: Energy = 1 (nothing happens)
- Minute 2: Energy = 2 → Break lock requiring 2 energy (X becomes 2)
- Minute 3: Energy = 2 (nothing happens)
- Minute 4: Energy = 4 → Break lock requiring 4 energy (X becomes 3)
- Minute 5: Energy = 3 (nothing happens)
- Minute 6: Energy = 6 → Break lock requiring 6 energy
- Total time: 6 minutes
Now Your Turn!
Pick the correct output for the given inputInput: strength = [5, 1, 8, 4]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- n == strength.length
- 1 <= n <= 80
- 1 <= strength[i] <= 106
- n == strength.length