|
| 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