日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[leetcode]488. Zuma Game

發布時間:2023/12/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [leetcode]488. Zuma Game 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:https://leetcode.com/problems/zuma-game/description/

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.

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.

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 -> WWInput: "WWRRBBWW", "WRBRW" Output: 2 Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> emptyInput:"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:

  • 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.
  • The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input.
  • The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input.
  • Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.

  • 方法一:(dfs)

    class Solution { public:int findMinStep(string board, string hand) {list<Node> board_list;vector<int> hand_count(CHAR_MAX, 0);int remain_in_hand = 0;for (int i = 0; i < board.length(); ++i) {board_list.push_back(Node(board[i], 1));if (i != board.length() - 1 && board[i] == board[i + 1]) {board_list.back().count++;i++;}}for (char ch : hand) {hand_count[ch]++;remain_in_hand ++;}int ret = DFS(board_list, hand_count, remain_in_hand);return ret == kMaxNeed ? -1 : ret;} private:// the kMaxNeed need to be greater than 5; const int kMaxNeed=6;struct Node{char ch;int count;Node(char c,int i):ch(c),count(i){}};void CleanBoard(list<Node> &board){auto it = board.begin();while (it != board.end()) {if (it->count >= 3) {it = board.erase(it);// Ensure iterator != end(), before you use the it// Ensure iterator != begin(), before invoking prev(it)if (it != board.begin() &&it != board.end() &&it->ch == prev(it)->ch){it->count += prev(it)->count;board.erase(prev(it));}} else {it++;}}}int DFS(list<Node> board, vector<int> &hand, int remain_in_hand) {CleanBoard(board);if (board.size() == 0) return 0;//cannot remove all the ballsif (remain_in_hand <= 0) return kMaxNeed;int min_need = kMaxNeed;for (auto it = board.begin(); it != board.end(); ++it) {int need = 3 - it->count;if (hand[it->ch] >= need) {hand[it->ch] -= need;it->count += need;list<Node> next_board = board;int ret = DFS(next_board, hand, remain_in_hand - need);min_need = min(min_need, ret + need);it->count -= need;hand[it->ch] += need;}}return min_need;} };


    總結

    以上是生活随笔為你收集整理的[leetcode]488. Zuma Game的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。