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 leaststrength[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.
Return the minimum time required for Bob to break all n locks.
Input:ength =[3,4,1], k =1Output: 4Explanation:
Time | Energy | x | Action | Updated x
---|---|---|---|---0|0|1| Nothing |11|1|1| Break 3rd Lock |22|2|2| Nothing |23|4|2| Break 2nd Lock |34|3|3| Break 1st Lock |3The locks cannot be broken in less than 4 minutes; thus, the answer is4.
Input:ength =[2,5,4], k =2Output: 5Explanation:
Time | Energy | x | Action | Updated x
---|---|---|---|---0|0|1| Nothing |11|1|1| Nothing |12|2|1| Break 1st Lock |33|3|3| Nothing |34|6|3| Break 2nd Lock |55|5|5| Break 3rd Lock |7The locks cannot be broken in less than 5 minutes; thus, the answer is5.
Since the number of locks is small (n ≤ 8), we can try all possible orders to break the locks. For each order, we simulate the sword’s energy growth and update the factor after each lock. Bitmask DP helps avoid recomputation for the same set of broken locks and current factor.
classSolution {
public:int minTimeToBreakLocks(vector<int>& strength, int k) {
int n = strength.size();
vector<vector<int>> dp(1<<n, vector<int>(n*k+2, -1));
function<int(int,int)> dfs = [&](int mask, int x) {
if (mask == (1<<n)-1) return0;
if (dp[mask][x] !=-1) return dp[mask][x];
int ans = INT_MAX;
for (int i =0; i < n; ++i) {
if (!(mask & (1<<i))) {
int t = (strength[i]+x-1)/x;
ans = min(ans, t + dfs(mask|(1<<i), x+k));
}
}
return dp[mask][x] = ans;
};
returndfs(0, 1);
}
};
classSolution {
publicintminTimeToBreakLocks(int[] strength, int k) {
int n = strength.length;
int[][] dp =newint[1<<n][n*k+2];
for (int[] d : dp) Arrays.fill(d, -1);
return dfs(0, 1, strength, k, dp);
}
privateintdfs(int mask, int x, int[] strength, int k, int[][] dp) {
int n = strength.length;
if (mask == (1<<n)-1) return 0;
if (dp[mask][x]!=-1) return dp[mask][x];
int ans = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if ((mask & (1<<i)) == 0) {
int t = (strength[i]+x-1)/x;
ans = Math.min(ans, t + dfs(mask|(1<<i), x+k, strength, k, dp));
}
}
return dp[mask][x]= ans;
}
}
classSolution {
funminTimeToBreakLocks(strength: IntArray, k: Int): Int {
val n = strength.size
val dp = Array(1 shl n) { IntArray(n*k+2) { -1 } }
fundfs(mask: Int, x: Int): Int {
if (mask == (1 shl n) - 1) return0if (dp[mask][x] != -1) return dp[mask][x]
var ans = Int.MAX_VALUE
for (i in0 until n) {
if (mask and (1 shl i) ==0) {
val t = (strength[i]+x-1)/x
ans = minOf(ans, t + dfs(mask or (1 shl i), x+k))
}
}
dp[mask][x] = ans
return ans
}
return dfs(0, 1)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defminTimeToBreakLocks(self, strength: list[int], k: int) -> int:
n = len(strength)
from functools import lru_cache
@lru_cache(maxsize=None)
defdfs(mask: int, x: int) -> int:
if mask == (1<<n)-1: return0 ans = float('inf')
for i in range(n):
ifnot (mask & (1<<i)):
t = (strength[i]+x-1)//x
ans = min(ans, t + dfs(mask|(1<<i), x+k))
return ans
return dfs(0, 1)