当前位置:网站首页>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)
边栏推荐
猜你喜欢
距LiveVideoStackCon 2022 上海站开幕还有2天!
『百日百题 · 基础篇』备战面试,坚持刷题 第四话——循环语句!
CAS:908007-17-0_Biotin-azide _生物素叠氮化物
E-commerce data warehouse ODS layer-----log data loading
LyScript 实现应用层钩子扫描器
idea2021配置svn报错Cannot run program “svn“ (in directory “xxx“):CreateProcess error=2,系统找不到指定的文件
距LiveVideoStackCon 2022 上海站开幕还有3天!
【kali-漏洞利用】(3.2)Metasploit基础(上):基础知识
CAS:908007-17-0_Biotin-azide_Biotin azide
win10安装及配置Gradle
随机推荐
win10安装及配置Gradle
2022年全国职业院校技能大赛网络安全 B模块 任务十windows操作系统渗透测试 国赛原题
dataframe 多层索引 更换索引 df.swaplevel(axis=1)
Unification of east-west and north-south communications
如何基于WPF写一款数据库文档管理工具(二)
研究Fabric中Etcd的Raft应用
全球观之地理部分
解决npm -v查看npm版本出现npm WARN config global `--global`, `--local` are deprecated. Use `--location报错
什么密码,永远无法被黑客攻破?
Zero trust, which has been popular for more than ten years, why can't it be implemented?
From September 1st, my country has granted zero-tariff treatment to 98% of tax items from 16 countries including Togo
反射机制
IDaaS 是什么?一文说清它的价值
Diazo Biotin-PEG3-DBCO|重氮化合物修饰生物素-三聚乙二醇-二苯并环辛炔
VIM操作
HCIP第十四天
nxp官方uboot移植到野火开发板PRO(修改LCD部分和网络部分)
CAS: 773888-45-2_BIOTIN ALKYNE_Biotin-alkynyl
[kali-vulnerability exploitation] (3.2) Metasploit basics (on): basic knowledge
ES、Kibana 8.0安装