LeetCode691. 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.
分析
乍一看应该是个dp问题,但是不知道怎么建模,还是多做题目来积累经验吧。
首先是要明白题目的意思,从绕来绕去的描述中寻找题目真正要求的是什么。这题的输入是一组字符串,和一个目标字符串,对于给定的字符串可以取它任意的分割部分,同一个字符串可以使用多次,以此来组成目标字符串,求从输入的那组字符串中取最少的字符串个树来组成目标字符串。很繁琐的描述,剥离下无用信息其实就是从给定一个字符串集合S,以及一个目标字符串T.求使用S中字符串的最小个数,能够满足T需要的字符数。
利用数组下标与值的映射关系来存储每个sticker以及target string中的字符及其数目,并且使用回溯来遍历所有的可能。
看了下递归遍历的代码,还是不是很懂,还是去看了下dp的解法。利用dp加上回溯递归的方法遍历求解所有的可能。对于每一个sticker,应用到组成target string的话,组成了一部分,还剩下了一部分,这样递归求解即可。
利用一个map存储dp数组,key是要组成的target string,值是最优解,也就最少使用多少个sticker。
dp[s] is the minimum stickers required for string s (-1 if impossible). Note s is sorted.
clearly, dp[""] = 0, and the problem asks for dp[target].
状态转移方程:
dp[s] = min(1+dp[reduced_s]) for all stickers,
here reduced_s is a new string after certain sticker applied
上面的意思是对于s我们循环从所有stickers中选则一个来组成它的一部分,那么它剩下的部分还要继续组成,这就成了相同的问题。递归求解即可,多说无益直接上代码来分析:
class Solution {
public int minStickers(String[] stickers, String target) {
int m = stickers.length;
int[][] mp = new int[m][26];
Map<String, Integer> dp = new HashMap<>();
for (int i = 0; i < m; i++)
for (char c:stickers[i].toCharArray()) mp[i][c-'a']++; // m行代表m个sticker,0~25列代表字母a~z,实际上将字符串中的字符按顺序排列了
dp.put("", 0);
return helper(dp, mp, target);
}
// mp数组存储每个sticker的字符及其数量,target是当前递归层次中要组成的目标字符串
private int helper(Map<String, Integer> dp, int[][] mp, String target) {
if (dp.containsKey(target)) return dp.get(target);
int ans = Integer.MAX_VALUE, n = mp.length;
int[] tar = new int[26];
for (char c:target.toCharArray()) tar[c-'a']++; // 存储组成目标字符串所需要的字符及其数量
// 对于每个sticker尝试将其作为target string的组成部分,递归求解
for (int i = 0; i < n; i++) {
// 这里的优化很有意思,使得整个循环从包含target string中的第一个字符的sticker开始,这样会减少计算
if (mp[i][target.charAt(0)-'a'] == 0) continue;
StringBuilder sb = new StringBuilder(); // 用来存储剩下要组成的target字符串,也就是参与下一轮递归的部分
// 对于当前的sticker,从a~z匹配
for (int j = 0; j < 26; j++) {
// 如果target string需要某个数量的字符而当前sticker中有一定数量的这个字符,将需要的数量减去已有的数量便是剩下还需要的这个字符的数量,在下轮递归中继续寻找
if (tar[j] > 0 )
for (int k = 0; k < Math.max(0, tar[j]-mp[i][j]); k++)
sb.append((char)('a'+j));
}
String s = sb.toString();
int tmp = helper(dp, mp, s); // 将剩下要组成的部分作为新的target string传给下层迭代,因为sticker可以重复利用所以mp不动
if (tmp != -1) ans = Math.min(ans, 1+tmp);
}
dp.put(target, ans == Integer.MAX_VALUE? -1:ans);
return dp.get(target);
}
}
上面那个优化是很有意思的,因为如果target string能由所有stickers拼出来的话,那么包含了target中的第一个字符的所有sticker至少有一个是在最后的结果中的,那么反过来想,我们在由stickers去遍历所有可能的时候可以直接从包含了target string中第一个字符的sticker出发。这题还是比较难的,首先用map来存储了dp数组,其次是用到了dp+递归的方式来遍历。
LeetCode691. Stickers to Spell Word的更多相关文章
- [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 ...
- [LeetCode] Stickers to Spell Word 贴片拼单词
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- 691. Stickers to Spell Word
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- LeetCode 691. Stickers to Spell Word
原题链接在这里:https://leetcode.com/problems/stickers-to-spell-word/ 题目: We are given N different types of ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 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 ...
随机推荐
- SDOI 2019 Round1 游记
\(SDOI~2019 ~ Round1\) 游记 \(Day ~0\) 报道.骑车子去的,好热.到了之后看到好几个同校神仙,还从那里莫名其妙的等了一会,然后交了钱签了名就拿挂牌走人了.现在居然还有受 ...
- c语言条件编译和预编译
转自http://www.cnblogs.com/rusty/archive/2011/03/27/1996806.html 一.C语言由源代码生成的各阶段如下: C源程序->编译预处理-> ...
- ALC662 在Mac中的安装
最近在装黑苹果,一切还算顺利(整了两周),就是ICH7的ALC662一直无法驱动成功.经过两天的爬文,终于成功了.以下是我的一点经验. 我装的是最新的10.9.2,显卡驱动是自带的(我的显卡为GT62 ...
- dev代码拷贝中文乱码的解决方案
.c / .cpp文件用记事本打开,再拷贝
- 利用机器学习实现微信小程序-加减大师自动答题
之前有看到微信小程序<跳一跳>别人用python实现自动运行,后来看到别人用hash码实现<加减大师>的自动答题领取娃娃,最近一直在研究深度学习,为啥不用机器学习实现呢?不就是 ...
- redis 新开端口号
2012 ps aux | grep redis 2013 cd /usr/local/redis/ 2014 ls 2015 cd etc/ 2016 ls 2017 cp redis.conf r ...
- 并发库应用之三 & 线程池与定时器应用
在TCP服务器编程模型的原理,每一个客户端连接用一个单独的线程为之服务,当与客户端的会话结束时,线程也就结束了,即每来一个客户端连接,服务器端就要创建一个新线程.如果访问服务器的客户端很多,那么服务器 ...
- 总结: 《jQuery基础教程》 5-完结
第5章:操作DOM HTML属性和DOM属性:attr()和prop() 获取表单控件的值:val() DOM操作方法的归纳: (1) 要在HTML中创建新元素,使用$()函数.(2) 要在每个匹配的 ...
- Hadoop生态圈-hive编写自定义函数
Hadoop生态圈-hive编写自定义函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- java基础-数组的折半查找原理
java基础-数组的折半查找原理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 如果让你写一个数组的查找功能,需求如下:在一个数组中,找一个元素,是否存在于数组中, 如果存在就返回 ...