LeetCode每日一题(488. Zuma Game)

这篇博客讨论了LeetCode上的一种Zuma游戏变体,目标是清除板上的所有彩色球。玩家手握若干彩色球,每次可以将一个球插入板上两个球之间或两端,若形成3个及以上同色连续球,则消除这些球。如果无法清除所有球,返回-1。例如,当board='WRRBBW',hand='RB'时,无法获胜。而board='WWRRBBWW', hand='WRBRW'时,可通过两次插入获胜。文章介绍了解题思路和如何计算最少插入球数。" 129481354,17527280,华为OD机试题解:积木最远距离,"['面试', '算法', 'C++', '数据结构']
摘要由CSDN通过智能技术生成

You are playing a variation of the game Zuma.

In this variation of Zuma, there is a single row of colored balls on a board, where each ball can be colored red ‘R’, yellow ‘Y’, blue ‘B’, green ‘G’, or white ‘W’. You also have several colored balls in your hand.

Your goal is to clear all of the balls from the board. On each turn:

Pick any ball from your hand and insert it in between two balls in the row or on either end of the row.
If there is a group of three or more consecutive balls of the same color, remove the group of balls from the board.
If this removal causes more groups of three or more of the same color to form, then continue removing each group until there are none left.
If there are no more balls on the board, then you win the game.
Repeat this process until you either win or do not have any more balls in your hand.
Given a string board, representing the row of balls on the board, and a string hand, representing the balls in your hand, return the minimum number of balls you have to insert to clear all the balls from the board. If you cannot clear all the balls from the board using the balls in your hand, return -1.

Example 1:

Input: board = “WRRBBW”, hand = “RB”
Output: -1

Explanation: It is impossible to clear all the balls. The best you can do is:

  • Insert ‘R’ so the board becomes WRRRBBW. WRRRBBW -> WBBW.
  • Insert ‘B’ so the board becomes WBBBW. WBBBW -> WW.
    There are still balls remaining on the board, and you are out of balls to insert.

Example 2:

Input: board = “WWRRBBWW”, hand = “WRBRW”
Output: 2

Explanation: To make the board empty:

  • Insert ‘R’ so the board becomes WWRRRBBWW. WWRRRBBWW -> WWBBWW.
  • Insert ‘B’ so the board becomes WWBBBWW. WWBBBWW -> WWWW -> empty.
    2 balls from your hand were needed to clear the board.

Example 3:

Input: board = “G”, hand = “GGGGG”
Output: 2

Explanation: To make the board empty:

  • Insert ‘G’ so the board becomes GG.
  • Insert ‘G’ so the board becomes GGG. GGG -> empty.
    2 balls from your hand were needed to clear the board.

Constraints:

  • 1 <= board.length <= 16
  • 1 <= hand.length <= 5
  • board and hand consist of the characters ‘R’, ‘Y’, ‘B’, ‘G’, and ‘W’.
  • The initial row of balls on the board will not have any groups of three or more consecutive balls of the same color.

头疼, 直接看代码吧



use std::collections::HashMap;

impl Solution {
   
    fn dp(board: Vec<char>, hand: Vec<char>, cache: &mut HashMap<(Vec<char>, Vec<char>), 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值