|
| 1 | +/** |
| 2 | + * Candy Crush |
| 3 | + * |
| 4 | + * This question is about implementing a basic elimination algorithm for Candy Crush. |
| 5 | + * |
| 6 | + * Given a 2D integer array board representing the grid of candy, different positive integers board[i][j] |
| 7 | + * represent different types of candies. A value of board[i][j] = 0 represents that the cell at position (i, j) |
| 8 | + * is empty. The given board represents the state of the game following the player's move. |
| 9 | + * |
| 10 | + * Now, you need to restore the board to a stable state by crushing candies according to the following rules: |
| 11 | + * |
| 12 | + * If three or more candies of the same type are adjacent vertically or horizontally, "crush" them all at the |
| 13 | + * same time - these positions become empty. |
| 14 | + * |
| 15 | + * After crushing all candies simultaneously, if an empty space on the board has candies on top of itself, |
| 16 | + * then these candies will drop until they hit a candy or bottom at the same time. (No new candies will drop |
| 17 | + * outside the top boundary.) |
| 18 | + * |
| 19 | + * After the above steps, there may exist more candies that can be crushed. If so, you need to repeat the above steps. |
| 20 | + * |
| 21 | + * If there does not exist more candies that can be crushed (ie. the board is stable), then return the current board. |
| 22 | + * You need to perform the above rules until the board becomes stable, then return the current board. |
| 23 | + * |
| 24 | + * Note: |
| 25 | + * The length of board will be in the range [3, 50]. |
| 26 | + * The length of board[i] will be in the range [3, 50]. |
| 27 | + * Each board[i][j] will initially start as an integer in the range [1, 2000]. |
| 28 | + */ |
| 29 | + |
| 30 | +/** |
| 31 | + * @param {number[][]} board |
| 32 | + * @return {number[][]} |
| 33 | + */ |
| 34 | +const candyCrush = board => { |
| 35 | + const m = board.length; |
| 36 | + const n = board[0].length; |
| 37 | + |
| 38 | + let shouldContinue = false; |
| 39 | + |
| 40 | + // Crush horizontally |
| 41 | + for (let i = 0; i < m; i++) { |
| 42 | + for (let j = 0; j < n - 2; j++) { |
| 43 | + const v = Math.abs(board[i][j]); |
| 44 | + if (v && v === Math.abs(board[i][j + 1]) && v === Math.abs(board[i][j + 2])) { |
| 45 | + board[i][j] = board[i][j + 1] = board[i][j + 2] = -v; |
| 46 | + shouldContinue = true; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Crush vertically |
| 52 | + for (let i = 0; i < m - 2; i++) { |
| 53 | + for (let j = 0; j < n; j++) { |
| 54 | + const v = Math.abs(board[i][j]); |
| 55 | + if (v && v === Math.abs(board[i + 1][j]) && v === Math.abs(board[i + 2][j])) { |
| 56 | + board[i][j] = board[i + 1][j] = board[i + 2][j] = -v; |
| 57 | + shouldContinue = true; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Drop vertically |
| 63 | + for (let j = 0; j < n; j++) { |
| 64 | + let row = m - 1; |
| 65 | + for (let i = m - 1; i >= 0; i--) { |
| 66 | + if (board[i][j] >= 0) { |
| 67 | + board[row--][j] = board[i][j]; |
| 68 | + } |
| 69 | + } |
| 70 | + for (let i = row; i >= 0; i--) { |
| 71 | + board[i][j] = 0; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return shouldContinue ? candyCrush(board) : board; |
| 76 | +}; |
| 77 | + |
| 78 | +export { candyCrush }; |
0 commit comments