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>), i32>) -> i32 {
if board.is_empty() {
return 0;
}
if hand.is_empty() {
return i32::MAX;
}
let mut ans = i32::MAX;
for i in 0..hand.len() {
if i == 0 || hand[i] != hand[i - 1] {
let mut next_hand = hand.clone();
let c = next_hand.remove(i);
for j in 0..board.len() {
// 重点就这一句, 该条件可以减少很多无效的尝试, 从而保证运行不会超时
if board[j] == c || j > 0 && board[j] == board[j - 1] {
let mut next_board = board.clone();
next_board.insert(j, c);
next_board = Solution::brust(next_board);
let next_res = if let Some(c) = cache.get(&(next_board.clone(), next_hand.clone())) {
*c
} else {
Solution::dp(next_board.clone(), next_hand.clone(), cache)
};
ans = ans.min(next_res);
}
}
}
}
if ans < i32::MAX {
ans += 1;
}
cache.insert((board, hand), ans);
ans
}
fn brust(board: Vec<char>) -> Vec<char> {
let mut l = Vec::new();
'outer: for c in board {
while let Some((lc, ln)) = l.pop() {
if lc == c {
l.push((lc, ln + 1));
continue 'outer;
}
if ln < 3 {
l.push((lc, ln));
break;
}
}
l.push((c, 1));
}
if let Some((lc, ln)) = l.pop() {
if ln < 3 {
l.push((lc, ln));
}
}
l.into_iter().map(|(c, n)| vec![c; n]).flatten().collect()
}
pub fn find_min_step(board: String, hand: String) -> i32 {
let board: Vec<char> = board.chars().collect();
let hand: Vec<char> = hand.chars().collect();
let ans = Solution::dp(board, hand, &mut HashMap::new());
if ans < i32::MAX {
return ans;
}
-1
}
}

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

被折叠的 条评论
为什么被折叠?



