691. Stickers to Spell Word
We are given N different types of stickers. Each sticker has a lowercase English word on it.
You would like to spell out the given target
string by cutting individual letters from your collection of stickers and rearranging them.
You can use each sticker more than once if you want, and you have infinite quantities of each sticker.
What is the minimum number of stickers that you need to spell out the target
? If the task is impossible, return -1.
Example 1:
Input:
["with", "example", "science"], "thehat"
Output:
3
Explanation:
We can use 2 "with" stickers, and 1 "example" sticker.
After cutting and rearrange the letters of those stickers, we can form the target "thehat".
Also, this is the minimum number of stickers necessary to form the target string.
Example 2:
Input:
["notice", "possible"], "basicbasic"
Output:
-1
Explanation:
We can't form the target "basicbasic" from cutting letters from the given stickers.
Note:
stickers
has length in the range[1, 50]
.stickers
consists of lowercase English words (without apostrophes).target
has length in the range[1, 15]
, and consists of lowercase English letters.- In all test cases, all words were chosen randomly from the 1000 most common US English words, and the target was chosen as a concatenation of two random words.
- The time limit may be more challenging than usual. It is expected that a 50 sticker test case can be solved within 35ms on average.
Approach #1: DP. [C++]
class Solution {
public:
int minStickers(vector<string>& stickers, string target) {
int m = stickers.size();
vector<vector<int>> mp(m, vector<int>(26, 0));
unordered_map<string, int> dp;
for (int i = 0; i < m; ++i) {
for (char c : stickers[i])
mp[i][c-'a']++;
}
dp[""] = 0;
return helper(dp, mp, target);
} private:
int helper(unordered_map<string, int>& dp, vector<vector<int>>& mp, string target) {
if (dp.count(target)) return dp[target];
int ans = INT_MAX, n = mp.size();
vector<int> tar(26, 0);
for (char c : target) tar[c-'a']++;
for (int i = 0; i < n; ++i) {
if (mp[i][target[0]-'a'] == 0) continue;
string s;
for (int j = 0; j < 26; ++j)
if (tar[j] - mp[i][j] > 0)
s += string(tar[j]-mp[i][j], 'a'+j);
int tmp = helper(dp, mp, s);
if (tmp != -1) ans = min(ans, 1+tmp);
}
dp[target] = ans == INT_MAX ? -1 : ans;
return dp[target];
}
};
Analysis:
https://leetcode.com/problems/stickers-to-spell-word/discuss/108318/C%2B%2BJavaPython-DP-%2B-Memoization-with-optimization-29-ms-(C%2B%2B)
691. Stickers to Spell Word的更多相关文章
- LeetCode 691. Stickers to Spell Word
原题链接在这里:https://leetcode.com/problems/stickers-to-spell-word/ 题目: We are given N different types of ...
- [LeetCode] Stickers to Spell Word 贴片拼单词
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- [Swift]LeetCode691. 贴纸拼词 | Stickers to Spell Word
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- LeetCode691. Stickers to Spell Word
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
随机推荐
- php获取远程图片模拟post,file上传到指定服务器
1.获取远程图片 /** $path保存图片的地址 $url要获取的远程图片地址 **/ function getimg($path,$url){ $aext = explode('.', $url) ...
- 微信小程序开发注意点和坑集
开发(Tips) 避开频繁setData * 小程序端对于频繁的逻辑层和显示层的交互很不友好,特别是安卓机,与浏览器上js直接操作DOM不同,小程序通过逻辑更新显示层并不完全实时,开发者应避免出现 ...
- Presto资源组配置
{ "rootGroups": [ { "name": "global", "softMemoryLimit": &qu ...
- Excel VBA入门(五)Excel对象操作
本章是本系列教程的重点.但我觉得应该不是难点.从第零章开始到学完本章,应该可以把VBA用于实战中了. Excel对象主要有4个: 工作薄 Workbook 工作表 Worksheet 单元格区域 Ra ...
- 刷题向》一道逆向思维题(BZOJ1046)(NORMAL)
这道题对于一类题都有一个通用思路:反向递减序列即为正向字典序. 对于逆向思维的题还要多做才能培养这种对于逆向思维的感觉. 想到这种方法之后,就很简单了. 因为n×m不会炸,所以反向LIS叠一个贪心就能 ...
- Hibernate核心API
------------------------siwuxie095 (一)Configuration 1.一般情况 或: 加载核心配置文件:在 src 下找到名称为 Hibernate.cfg.xm ...
- RTX Server SDK跨服务器如何调用
1. 确认安装RTX Server SDK在开发的机器上必须确认已经安装了RTX Server SDK,并且与RTX Server的版本要一致.该计算机后面我们简称SDK计算机. 2. 步骤2 ...
- 智能合约调用另一合约中的payable方法
参考链接: https://ethereum.stackexchange.com/questions/9705/how-can-you-call-a-payable-function-in-anoth ...
- SQLSERVER Tempdb的作用及优化
tempdb 系统数据库是可供连接到 SQL Server 实例的所有用户使用的全局资源.tempdb 数据库用于存储下列对象:用户对象.内部对象和版本存储区. 用户对象 用户对象由用户显式创建.这些 ...
- 冲刺NOIP2015提高组复赛模拟试题(五)2.道路修建
2.道路修建 描述 Description liouzhou_101最悲痛的回忆就是NOI2011的道路修建,当时开了系统堆栈,结果无限RE… 出于某种报复心理,就把那题神奇了一下: 在 Z星球上有N ...