[Swift]LeetCode488. 祖瑪遊戲 | Zuma Game

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-acjavzcu-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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

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.數組

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] -> empty微信

Note:函數

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

回憶一下祖瑪遊戲。如今桌上有一串球,顏色有紅色(R),黃色(Y),藍色(B),綠色(G),還有白色(W)。 如今你手裏也有幾個球。this

每一次,你能夠從手裏的球選一個,而後把這個球插入到一串球中的某個位置上(包括最左端,最右端)。接着,若是有出現三個或者三個以上顏色相同的球相連的話,就把它們移除掉。重複這一步驟直到桌上全部的球都被移除。spa

找到插入並能夠移除掉桌上全部球所需的最少的球數。若是不能移除桌上全部的球,輸出 -1 。翻譯

示例:
輸入: "WRRBBW", "RB" 
輸出: -1 
解釋: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW (翻譯者標註:手上球已經用完,桌上還剩兩個球沒法消除,返回-1)

輸入: "WWRRBBWW", "WRBRW" 
輸出: 2 
解釋: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty

輸入:"G", "GGGGG" 
輸出: 2 
解釋: G -> G[G] -> GG[G] -> empty 

輸入: "RBYYBBRRB", "YRBGB" 
輸出: 3 
解釋: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty 

標註:code

  1. 你能夠假設桌上一開始的球中,不會有三個及三個以上顏色相同且連着的球。
  2. 桌上的球不會超過20個,輸入的數據中表明這些球的字符串的名字是 "board" 。
  3. 你手中的球不會超過5個,輸入的數據中表明這些球的字符串的名字是 "hand"。
  4. 輸入的兩個字符串均爲非空字符串,且只包含字符 'R','Y','B','G','W'。

Runtime:  1004 ms
Memory Usage: 4 MB
 1 class Solution {
 2     func findMinStep(_ board: String, _ hand: String) -> Int {
 3         var res:Int = Int.max
 4         var s:Set<Character> = Set<Character>()
 5         for i in 0..<hand.count
 6         {
 7             if s.contains(hand[i]) {continue}
 8             s.insert(hand[i])
 9             for j in 0..<board.count
10             {
11                 if board[j] != hand[i] {continue}
12                 var newBoard = board
13                 var newHand = hand
14                 let index1 = newBoard.index(newBoard.startIndex, offsetBy: j)
15                 newBoard.insert(hand[i],at:index1)
16                 newBoard = removeConsecutive(newBoard)
17                 if newBoard.count == 0 {return 1}
18                 let index2 = newHand.index(newHand.startIndex, offsetBy: i)
19                 newHand.remove(at:index2)
20                 var cnt:Int = findMinStep(newBoard, newHand)
21                 if cnt != -1
22                 {
23                     res = min(res, cnt + 1)
24                 }
25             }
26         }
27         return res == Int.max ? -1 : res
28     }
29     
30     func removeConsecutive(_ board:String) -> String
31     {
32         var j:Int = 0
33         for i in 0...board.count
34         {
35             if i < board.count && board[i] == board[j] {continue}
36             if i - j >= 3 {return removeConsecutive(board.subString(0, j) + board.subString(i))}
37             else{j = i}
38         }
39         return board
40     }
41 }
42 
43 extension String {        
44     //subscript函數能夠檢索數組中的值
45     //直接按照索引方式截取指定索引的字符
46     subscript (_ i: Int) -> Character {
47         //讀取字符
48         get {return self[index(startIndex, offsetBy: i)]}
49     }
50     
51     // 截取字符串:指定索引和字符數
52     // - begin: 開始截取處索引
53     // - count: 截取的字符數量
54     func subString(_ begin:Int,_ count:Int) -> String {
55         let start = self.index(self.startIndex, offsetBy: max(0, begin))
56         let end = self.index(self.startIndex, offsetBy:  min(self.count, begin + count))
57         return String(self[start..<end]) 
58     }
59     
60     // 截取字符串:從index到結束處
61     // - Parameter index: 開始索引
62     // - Returns: 子字符串
63     func subString(_ index: Int) -> String {
64         let theIndex = self.index(self.endIndex, offsetBy: index - self.count)
65         return String(self[theIndex..<endIndex])
66     }
67 }
相關文章
相關標籤/搜索