当前位置:网站首页>488. Zuma Game
488. Zuma Game
2022-08-03 21:46:00 【51CTO】
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand.
Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed.
Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1.
Note:
You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color.
The number of balls on the table won’t exceed 20, and the string represents these balls is called “board” in the input.
The number of balls in your hand won’t exceed 5, and the string represents these balls is called “hand” in the input.
Both input strings will be non-empty and only contain characters ‘R’,’Y’,’B’,’G’,’W’.
思路:
题目希望我们用最少的球来消掉桌上所有的球,如果不能完全消掉,返回-1。
我们使用哈希表来统计手中每种球的个数。
然后我们遍历桌上的球,我们找连续相同球的个数,在没有可以消除的情况下,连续的个数只能是1个或2个,然后我们用3减去连续个数,就是我们需要补充的球数以使其可以被消除,那么我们在哈希表表中看我们手中的该类型的球够不够,如果够就表示可以消除,我们在哈希表中减去需要使用掉的球数,然后将消掉的球移除。
然后对新的字符串调用递归,如果可以成功消除,会返回一个结果,该结果加上之前需要的球数用来更新结果res,注意调用完递归要恢复哈希表的状态(backtrack)。
Time Complexity: O(N) Space Complexity: O(N)
边栏推荐
- XSS线上靶场---haozi
- 深度学习和机器学习有什么区别?
- [3D检测系列-PV-RCNN] PV-RCNN论文详解、PV-RCNN代码复现、包含官网PV-RCNN预训练权重及报错问题
- nxp官方uboot移植到野火开发板PRO(修改LCD部分和网络部分)
- CAS:1797415-74-7_TAMRA-Azide-PEG-Biotin
- 这几个常用 alias,带你高效做事(下)
- 从0到1看支付
- 2022年全国职业院校技能大赛网络安全 B模块 B-1任务一:主机发现与信息收集 国赛原题
- FVCOM 3D Numerical Simulation of Hydrodynamics, Water Exchange, Dispersion and Transport of Oil Spills丨FVCOM Model Watershed, Numerical Simulation Method of Marine Water Environment
- 软件测试人员必备的60个测试工具清单,建议收藏一波~
猜你喜欢
随机推荐
LVS负载均衡集群
CAS:1797415-74-7_TAMRA-Azide-PEG-Biotin
VLAN实验
服务器安装redis
一体化HTAP数据库如此难,为什么他们还要做?
深度学习和机器学习有什么区别?
主板设计中:网络变压器与RJ45网口之间应该保持什么样的距离?
384. Shuffle an Array
XSS测试
Nacos配置文件管理、微服务获取Nacos配置文件
2021年数据泄露成本报告解读
剑指 Offer 07. 重建二叉树
C. Divan and bitwise operations
Interesting opencv - record image binarization and similarity
LeetCode_Digit Statistics_Medium_400. Nth Digit
tidyverse based on data.table?
Zero trust, which has been popular for more than ten years, why can't it be implemented?
6. XML
CAS:1620523-64-9_Azide-SS-biotin_biotin-disulfide-azide
小朋友学C语言(1):Hello World









