当前位置:网站首页>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)
边栏推荐
- 码率vs.分辨率,哪一个更重要?
- 图神经网络怎么入门?一文带你了解图神经网络入门路径-GNN入门
- A. Color the Picture- Codeforces Round #810 (Div. 1)
- XSS holes emersion
- StoneDB 开源社区月刊 | 202207期
- 易基因:植物宏病毒组研究:植物病毒的进化与生态 | 顶刊综述
- [kali-vulnerability scanning] (2.1) Nessus lifts IP restrictions, scans quickly without results, and plugins are deleted (middle)
- 382. Linked List Random Node
- LeetCode_Digit Statistics_Medium_400. Nth Digit
- LVS负载均衡集群
猜你喜欢
XSS online shooting range---Warmups
《强化学习周刊》第56期:GraphIRL、REDEEMER & 眼科强化学习的潜在研究
XSS测试
【Odoo】硬核组件开发,全文没一句废话~
Several difficult problems in DDD
【历史上的今天】8 月 3 日:微软研究院的创始人诞生;陌陌正式上线;苹果发布 Newton OS
Zero trust, which has been popular for more than ten years, why can't it be implemented?
[3D检测系列-PV-RCNN] PV-RCNN论文详解、PV-RCNN代码复现、包含官网PV-RCNN预训练权重及报错问题
CAS:1260586-88-6_Biotin-C5-Azide_Biotin-C5-Azide
一些思考:腾讯股价为何持续都低
随机推荐
主板设计中:网络变压器与RJ45网口之间应该保持什么样的距离?
From September 1st, my country has granted zero-tariff treatment to 98% of tax items from 16 countries including Togo
386. Lexicographical Numbers
模板字符串
Cross-end development technical reserve record
Unification of east-west and north-south communications
嵌入式开发:嵌入式基础——代码和数据空间揭秘
距LiveVideoStackCon 2022 上海站开幕还有3天!
研究Fabric中Etcd的Raft应用
手动输入班级人数及成绩求总成绩和平均成绩?
E - Swap
七夕快乐!
CC2530_ZigBee+华为云IOT:设计一套属于自己的冷链采集系统
CAS:1192802-98-4_UV 裂解的生物素-PEG2-叠氮
关于GPIO你真的懂了吗?这篇文章都给你整理好了
云图说丨初识华为云微服务引擎CSE
深度学习和机器学习有什么区别?
4. 模块化编程
idea2021.1.3配置Gradle步骤
XSS practice - cycle and two cycle problem at a time