691. Stickers to Spell Word
https://leetcode.com/problems/stickers-to-spell-word/
/*
* @lc app=leetcode id=691 lang=cpp
*
* [691] Stickers to Spell Word
*/
// @lc code=start
class Solution {
public:
int minStickers(vector<string>& stickers, string target) {
const int N = target.size(), M = 1 << N; // bitmap
vector<uint> dp(M, -1);
dp[0] = 0;
for (int i = 0; i < M; ++i) {
if (dp[i] == -1) continue;
for (string &s : stickers) {
int next = i;
for (const auto c : s) {
for (int j = 0; j < N; ++j) {
if ((next >> j & 1) == 0 && target[j] == c) {
next |= 1 << j;
break;
}
}
}
dp[next] = min(dp[next], dp[i] + 1);
}
}
return dp[M - 1];
}
};
// @lc code=end
Previous1066. Campus Bikes IINext1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
Last updated