原题链接在这里:https://leetcode.com/problems/candy-crush/

题目:

This question is about implementing a basic elimination algorithm for Candy Crush.

Given a 2D integer array board representing the grid of candy, different positive integers board[i][j] represent different types of candies. A value of board[i][j] = 0 represents that the cell at position (i, j) is empty. The given board represents the state of the game following the player's move. Now, you need to restore the board to a stable state by crushing candies according to the following rules:

  1. If three or more candies of the same type are adjacent vertically or horizontally, "crush" them all at the same time - these positions become empty.
  2. After crushing all candies simultaneously, if an empty space on the board has candies on top of itself, then these candies will drop until they hit a candy or bottom at the same time. (No new candies will drop outside the top boundary.)
  3. After the above steps, there may exist more candies that can be crushed. If so, you need to repeat the above steps.
  4. If there does not exist more candies that can be crushed (ie. the board is stable), then return the current board.

You need to perform the above rules until the board becomes stable, then return the current board.

Example:

  1. Input:
  2. board =
  3. [[110,5,112,113,114],[210,211,5,213,214],[310,311,3,313,314],[410,411,412,5,414],[5,1,512,3,3],[610,4,1,613,614],[710,1,2,713,714],[810,1,2,1,1],[1,1,2,2,2],[4,1,4,4,1014]]
  4.  
  5. Output:
  6. [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[110,0,0,0,114],[210,0,0,0,214],[310,0,0,113,314],[410,0,0,213,414],[610,211,112,313,614],[710,311,412,613,714],[810,411,512,713,1014]]
  7.  
  8. Explanation:

Note:

  1. The length of board will be in the range [3, 50].
  2. The length of board[i] will be in the range [3, 50].
  3. Each board[i][j] will initially start as an integer in the range [1, 2000].

题解:

There are two steps.

Step 1: Mark 3 adjacent candies. Check if there are 3 adjacent candies. First check row by row, then column by column.

If there are, mark these values as negative.

Step 2: Crush them. Rewrite board with only positive numbers.

If there is crushing, that means there may be another round of crash, use recursion. Otherwise, there wouldn't be another round of crash, return the board.

Time Comlexity: O(M^2*n^2). m = board.length. n = board[0].length.

Each crash, there would be 3 crashed at minimum. Totally there are m*n candies. So recursion could run for m*n/3 times.

Each recursion, it takes O(m*n).

Space: O(1).

AC Java:

  1. class Solution {
  2. public int[][] candyCrush(int[][] board) {
  3. if(board == null || board.length == 0 | board[0].length == 0){
  4. return board;
  5. }
  6.  
  7. boolean todo = false;
  8. int m = board.length;
  9. int n = board[0].length;
  10. for(int i = 0; i<m; i++){
  11. for(int j = 0; j<n-2; j++){
  12. int val = Math.abs(board[i][j]);
  13. if(val!=0 && val==Math.abs(board[i][j+1]) && val==Math.abs(board[i][j+2])){
  14. todo = true;
  15. board[i][j] = board[i][j+1] = board[i][j+2] = -val;
  16. }
  17. }
  18. }
  19.  
  20. for(int j = 0; j<n; j++){
  21. for(int i = 0; i<m-2; i++){
  22. int val = Math.abs(board[i][j]);
  23. if(val!=0 && val==Math.abs(board[i+1][j]) && val==Math.abs(board[i+2][j])){
  24. todo = true;
  25. board[i][j] = board[i+1][j] = board[i+2][j] = -val;
  26. }
  27. }
  28. }
  29.  
  30. for(int j = 0; j<n; j++){
  31. int br = m-1;
  32. for(int i = m-1; i>=0; i--){
  33. if(board[i][j] > 0){
  34. board[br--][j] = board[i][j];
  35. }
  36. }
  37.  
  38. while(br>=0){
  39. board[br--][j] = 0;
  40. }
  41. }
  42.  
  43. return todo ? candyCrush(board) : board;
  44. }
  45. }

LeetCode 723. Candy Crush的更多相关文章

  1. [LeetCode] 723. Candy Crush 糖果消消乐

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

  2. [LeetCode] 723. Candy Crush 糖果粉碎

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

  3. 【LeetCode】723. Candy Crush 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

  4. [LeetCode] Candy Crush 糖果消消乐

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

  5. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  6. (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重

    原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...

  7. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

  8. 【leetcode】Candy(hard) 自己做出来了 但别人的更好

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  9. 【leetcode】Candy

    题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...

随机推荐

  1. 【LEETCODE】60、数组分类,适中级别,题目:75、560、105

    package y2019.Algorithm.array.medium; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.a ...

  2. 深入玩转K8S之外网如何访问业务应用

    有一个问题就是现在我的业务分配在多个Pod上,那么如果我某个Pod死掉岂不是业务完蛋了,当然也会有人说Pod死掉没问题啊,K8S自身机制Deployment和Controller会动态的创建和销毁Po ...

  3. maven的setting配置文件中mirror和repository的区别

    当maven需要到的依赖jar包不在本地仓库时, 就需要到远程仓库下载 . 这个时候如果mavensetting.xml中配置了镜像 , 而且镜像配置的规则中匹配到目标仓库时 , maven认为目标仓 ...

  4. Deploy custom service on non hadoop node with Apache Ambari

    1   I want to deploy a custom service onto non hadoop nodes using Apache Ambari. I have created a cu ...

  5. php数组的数学功能相关常用函数

    php数组中有一些函数与数学相关的函数,大多都是以array开头然后下划线接一个数学上的英文单词,如下: array_diff() array_diff_assoc() array_intersect ...

  6. GoogleMap增加标记和路线轨迹的方法

    声明:本文基于JavaScript环境编写. 前言 按照目前的项目需求,我们需要在谷歌地图上标记出当前仓库的位置.司机补货的行车路径.司机当前班次需要补货的机器的位置,同时根据补货状态的不同标记成不同 ...

  7. eyoucms 模板

    https://www.oschina.net/p/eyoucms 下载模板 https://www.eyoucms.com/doc/operation/ 学习手册

  8. redux核心知识

    Provider 作用:把父组件传递进来的store对象放入react 上下文中,这样connect组件就可以从上下文中获取到store对象   Connect 作用: 1.从react上下文中取出s ...

  9. Java 之 Session

    Session 一.概述 Session技术:服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器端的对象(HttpSession)中. 二.使用步骤 1.获取 HttpSession ...

  10. golang静态编译

    golang 的编译(不涉及 cgo 编译的前提下)默认使用了静态编译,不依赖任何动态链接库. 这样可以任意部署到各种运行环境,不用担心依赖库的版本问题.只是体积大一点而已,存储时占用了一点磁盘,运行 ...