-
Notifications
You must be signed in to change notification settings - Fork 3
/
691.cpp
32 lines (30 loc) · 1.03 KB
/
691.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
27
28
29
30
31
32
class Solution {
public:
int minStickers(vector<string>& stickers, string target) {
int n = target.size();
// use bit state to represent target
// dp[state] = min # of sticker needed
// 011000 => have h and e
// thehat
int m = 1 << n;
vector<int> dp(m, INT_MAX);
dp[0] = 0;
for (int state = 0; state < m; ++state) {
if (dp[state] == INT_MAX) continue;
// construct the next state
for (auto& sticker : stickers) {
int nextState = state;
for (auto c : sticker) {
for (int i = 0; i < n; ++i) {
if (c == target[i] && !((nextState >> i) & 1)) {
nextState |= (1 << i);
break;
}
}
}
dp[nextState] = min(dp[nextState], dp[state] + 1);
}
}
return (dp[m - 1] == INT_MAX) ? -1 : dp[m - 1];
}
};