488. Zuma Game

Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand.java

Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed.ui

Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1.this

 

Examples:
Input: "WRRBBW", "RB" Output: -1 Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW Input: "WWRRBBWW", "WRBRW" Output: 2 Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty Input:"G", "GGGGG" Output: 2 Explanation: G -> G[G] -> GG[G] -> empty Input: "RBYYBBRRB", "YRBGB" Output: 3 Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> emptycode

 

Note:htm

  1. You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color.
  2. The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input.
  3. The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input.
  4. Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.

 

Approach #1: DFS. [My Code][Wrong].blog

class Solution {
    int ret = Integer.MAX_VALUE;
    
    public int findMinStep(String board, String hand) {
        
        dfs(board, hand, 0);
        
        
        return ret == Integer.MAX_VALUE ? -1 : ret;
    }
    
    public void dfs(String board, String hand, int cur) {
        if (dontHave(hand) && board.length() != 0) return;
        if (board == null && board.length() == 0) 
            ret = Math.min(ret, cur);
        
        
        if (board.length() < 2 && inHand(hand, board.charAt(0))) {
            board += board.charAt(0);
            dfs(board, hand, cur+1);
        }
        
        for (int i = 1; i < board.length(); ++i) {
            StringBuilder copy = new StringBuilder(board);
            boolean isChange = false;

            if (board.charAt(i) == board.charAt(i-1) && inHand(hand, board.charAt(i))) {
                copy.deleteCharAt(i-1);
                copy.deleteCharAt(i-1);
                String temp = trim(copy);
                dfs(temp, hand, cur+1);
                isChange = true;
            }
            
            // recover
            if (isChange) {
                hand += board.charAt(i);
            }
        }
    }
    
    public String trim(StringBuilder temp) {
        if (temp == null || temp.length() == 0) return "";
        int count = 1;
        for (int i = 1; i < temp.length(); ++i) {
            if (temp.charAt(i-1) == temp.charAt(i)) {
                count++;
            } else {
                if (count >= 3) {
                    int lastPos = i - count;
                    return trim(temp.delete(lastPos, i));
                }
                count = 1;
            }
        }
        return temp.toString();
    }
    
    public boolean inHand(String hand, Character c) {
        for (int i = 0; i < hand.length(); ++i) {
            if (hand.charAt(i) == c) {
                hand.charAt(i) = '#';
                return true;
            }
        }
        return false;
    }
    
    public boolean dontHave(String hand) {
        for (int i = 0; i < hand.length(); ++i) {
            if (hand.charAt(i) != '#') return false;
        }
        return true;
    }
}

  

Approach #2: DFS. [Java]leetcode

class Solution {
    int MaxNum = 6;
    
    public int findMinStep(String board, String hand) {
        int ret = MaxNum;
        int[] handCount = new int[26];
        for (int i = 0; i < hand.length(); ++i) 
            handCount[hand.charAt(i) - 'A']++;
        
        ret = dfs(board+"#", handCount);
        
        return ret == MaxNum ? -1 : ret;
    }
    
    public int dfs(String s, int[] h) {
        s = removeConsecutives(s);
        if (s.equals("#")) return 0;
        int rs = MaxNum, need = 0;
        for (int i = 0, j = 0; j < s.length(); ++j) {
            if (s.charAt(i) == s.charAt(j)) continue;
            need = 3 - (j - i);
            if (h[s.charAt(i)-'A'] >= need) {
                h[s.charAt(i)-'A'] -= need;
                rs = Math.min(rs, need + dfs(s.substring(0, i) + s.substring(j), h));
                h[s.charAt(i)-'A'] += need;
            }
            i = j;
        }
        return rs;
    }
    
    public String removeConsecutives(String s) {
        for (int i = 0, j = 0; j < s.length(); ++j) {
            if (s.charAt(i) == s.charAt(j)) continue;
            if (j - i >= 3) {
                return removeConsecutives(s.substring(0, i) + s.substring(j));
            } else i = j;
        }
        return s;
    }
    
    
}

  

Reference:rem

https://leetcode.com/problems/zuma-game/discuss/97010/%22short%22-java-solution-beats-98get

相關文章
相關標籤/搜索