Algorithm


Problem Name: 488. Zuma Game

Problem Link: https://leetcode.com/problems/zuma-game/

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.

 
 

Code Examples

#1 Code Example with Javascript Programming

Code - Javascript Programming


const findMinStep = function(board, hand) {
  const map = {}
  for (let c of hand) map[c] = (map[c] || 0) + 1
  const res = helper(board, map)
  return res === Number.MAX_VALUE ? -1 : res
}

function helper(s, m) {
  const str = reduce(s)
  if (str.length === 0) return 0
  let res = Number.MAX_VALUE
  let i = 0
  while (i < str.length) {
    const beg = i
    while (i < str.length && str[i] === str[beg]) {
      i++
    }
    if (m[str[beg]] >= 3 - (i - beg)) {
      const dval = 3 - i + beg
      m[str[beg]] -= dval
      const tmp = helper(s.slice(0, beg) + s.slice(i), m)
      m[str[beg]] += dval
      if (tmp !== Number.MAX_VALUE) res = res < tmp + dval ? res : tmp + dval
    }
  }
  return res
}
function reduce(str) {
  let res = ''
  let i = 0
  while (i < str.length) {
    const beg = i
    while (i < str.length && str[beg] === str[i]) {
      i++
    }
    if (i - beg >= 3) {
      return reduce(str.slice(0, beg) + str.slice(i))
    }
  }
  return str
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
board = "WRRBBW", hand = "RB"

Output

x
+
cmd
-1

#2 Code Example with Python Programming

Code - Python Programming


class Solution:
    def findMinStep(self, board, hand):   
        def dfs(s, c):
            if not s: return 0
            res, i = float("inf"), 0
            while i < len(s):
                j = i + 1
                while j < len(s) and s[i] == s[j]: j += 1
                incr = 3 - (j - i)
                if c[s[i]] >= incr:
                    incr = 0 if incr < 0 else incr
                    c[s[i]] -= incr
                    tep = dfs(s[:i] + s[j:], c)
                    if tep >= 0: res = min(res, tep + incr)
                    c[s[i]] += incr
                i = j
            return res if res != float("inf") else -1
        return dfs(board, collections.Counter(hand))
Copy The Code & Try With Live Editor

Input

x
+
cmd
board = "WRRBBW", hand = "RB"

Output

x
+
cmd
-1
Advertisements

Demonstration


Previous
#486 Leetcode Predict the Winner Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#491 Leetcode Non-decreasing Subsequences Solution in C, C++, Java, JavaScript, Python, C# Leetcode