-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoln.cpp
26 lines (26 loc) · 810 Bytes
/
soln.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
int minStickers(vector<string>& stickers, string target) {
int n = target.length();
int N = (1 << n);
vector<int> dp(N, -1);
dp[0] = 0;
for(int i = 0; i < N; ++i) {
if (dp[i] == -1) continue;
for(auto & s : stickers) {
int now = i;
for(auto ch : s) {
for(int j = 0; j < n; ++j) {
if (target[j] == ch && !(now & (1 << j))) {
now |= (1 << j);
break;
}
}
}
if (dp[now] == -1) dp[now] = dp[i] + 1;
else dp[now] = min(dp[now], dp[i] + 1);
}
}
return dp.back();
}
};