Skip to content

Commit e8d8f51

Browse files
committedMar 3, 2025
Add solution #488
1 parent 2c4a331 commit e8d8f51

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@
391391
483|[Smallest Good Base](./0483-smallest-good-base.js)|Hard|
392392
485|[Max Consecutive Ones](./0485-max-consecutive-ones.js)|Easy|
393393
486|[Predict the Winner](./0486-predict-the-winner.js)|Medium|
394+
488|[Zuma Game](./0488-zuma-game.js)|Hard|
394395
491|[Non-decreasing Subsequences](./0491-non-decreasing-subsequences.js)|Medium|
395396
492|[Construct the Rectangle](./0492-construct-the-rectangle.js)|Easy|
396397
496|[Next Greater Element I](./0496-next-greater-element-i.js)|Easy|

‎solutions/0488-zuma-game.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* 488. Zuma Game
3+
* https://leetcode.com/problems/zuma-game/
4+
* Difficulty: Hard
5+
*
6+
* You are playing a variation of the game Zuma.
7+
*
8+
* In this variation of Zuma, there is a single row of colored balls on a board, where each ball
9+
* can be colored red 'R', yellow 'Y', blue 'B', green 'G', or white 'W'. You also have several
10+
* colored balls in your hand.
11+
*
12+
* Your goal is to clear all of the balls from the board. On each turn:
13+
* - Pick any ball from your hand and insert it in between two balls in the row or on either end
14+
* of the row.
15+
* - If there is a group of three or more consecutive balls of the same color, remove the group of
16+
* balls from the board.
17+
* - If this removal causes more groups of three or more of the same color to form, then continue
18+
* removing each group until there are none left.
19+
* - If there are no more balls on the board, then you win the game.
20+
* - Repeat this process until you either win or do not have any more balls in your hand.
21+
*
22+
* Given a string board, representing the row of balls on the board, and a string hand, representing
23+
* the balls in your hand, return the minimum number of balls you have to insert to clear all the
24+
* balls from the board. If you cannot clear all the balls from the board using the balls in your
25+
* hand, return -1.
26+
*/
27+
28+
/**
29+
* @param {string} board
30+
* @param {string} hand
31+
* @return {number}
32+
*/
33+
var findMinStep = function(board, hand) {
34+
const handCount = {};
35+
for (const ball of hand) {
36+
handCount[ball] = (handCount[ball] || 0) + 1;
37+
}
38+
39+
const memo = new Map();
40+
41+
function dfs(board, hand) {
42+
if (board.length === 0) return 0;
43+
if (Object.values(hand).every(count => count === 0)) return -1;
44+
const key = board + getHandString(hand);
45+
if (memo.has(key)) return memo.get(key);
46+
let minSteps = Infinity;
47+
48+
for (const color in hand) {
49+
if (hand[color] <= 0) continue;
50+
hand[color]--;
51+
for (let i = 0; i <= board.length; i++) {
52+
if (i > 0 && i < board.length) {
53+
const left = board[i-1];
54+
const right = board[i];
55+
if (left !== color && right !== color && left !== right) continue;
56+
}
57+
const newBoard = board.slice(0, i) + color + board.slice(i);
58+
const afterRemove = removeConsecutive(newBoard);
59+
const steps = dfs(afterRemove, hand);
60+
if (steps !== -1) {
61+
minSteps = Math.min(minSteps, steps + 1);
62+
}
63+
}
64+
hand[color]++;
65+
}
66+
const result = minSteps === Infinity ? -1 : minSteps;
67+
memo.set(key, result);
68+
return result;
69+
};
70+
71+
return dfs(board, handCount);
72+
};
73+
74+
function getHandString(hand) {
75+
return Object.entries(hand)
76+
.filter(([_, count]) => count > 0)
77+
.sort(([a], [b]) => a.localeCompare(b))
78+
.map(([color, count]) => color + count)
79+
.join('');
80+
}
81+
82+
function removeConsecutive(board) {
83+
let changed = true;
84+
while (changed) {
85+
changed = false;
86+
for (let i = 0; i < board.length;) {
87+
let j = i;
88+
while (j < board.length && board[j] === board[i]) {
89+
j++;
90+
}
91+
if (j - i >= 3) {
92+
board = board.substring(0, i) + board.substring(j);
93+
changed = true;
94+
i = 0;
95+
} else {
96+
i++;
97+
}
98+
}
99+
}
100+
return board;
101+
}

0 commit comments

Comments
 (0)
Please sign in to comment.