723. Candy Crush
C# solution
correspondant/originalpublic class Solution {
public int[][] CandyCrush(int[][] board) {
int m = board.Length;
int n = board[0].Length;
bool stable = false;
while (!stable) {
stable = true;
bool[][] crush = new bool[m][];
for (int i = 0; i < m; i++) {
crush[i] = new bool[n];
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n - 2; j++) {
int v = Math.Abs(board[i][j]);
if (v != 0 && v == Math.Abs(board[i][j + 1]) && v == Math.Abs(board[i][j + 2])) {
stable = false;
crush[i][j] = crush[i][j + 1] = crush[i][j + 2] = true;
}
}
}
for (int i = 0; i < m - 2; i++) {
for (int j = 0; j < n; j++) {
int v = Math.Abs(board[i][j]);
if (v != 0 && v == Math.Abs(board[i + 1][j]) && v == Math.Abs(board[i + 2][j])) {
stable = false;
crush[i][j] = crush[i + 1][j] = crush[i + 2][j] = true;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (crush[i][j]) {
board[i][j] = 0;
}
}
}
for (int j = 0; j < n; j++) {
int idx = m - 1;
for (int i = m - 1; i >= 0; i--) {
if (board[i][j] != 0) {
board[idx][j] = board[i][j];
idx--;
}
}
for (int i = idx; i >= 0; i--) {
board[i][j] = 0;
}
}
}
return board;
}
}
C++ solution
brouillon automatique, à relire avant soumission#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
public int[][] CandyCrush(int[][] board) {
int m = board.size();
int n = board[0].size();
bool stable = false;
while (!stable) {
stable = true;
bool[][] crush = new bool[m][];
for (int i = 0; i < m; i++) {
crush[i] = new bool[n];
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n - 2; j++) {
int v = abs(board[i][j]);
if (v != 0 && v == abs(board[i][j + 1]) && v == abs(board[i][j + 2])) {
stable = false;
crush[i][j] = crush[i][j + 1] = crush[i][j + 2] = true;
}
}
}
for (int i = 0; i < m - 2; i++) {
for (int j = 0; j < n; j++) {
int v = abs(board[i][j]);
if (v != 0 && v == abs(board[i + 1][j]) && v == abs(board[i + 2][j])) {
stable = false;
crush[i][j] = crush[i + 1][j] = crush[i + 2][j] = true;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (crush[i][j]) {
board[i][j] = 0;
}
}
}
for (int j = 0; j < n; j++) {
int idx = m - 1;
for (int i = m - 1; i >= 0; i--) {
if (board[i][j] != 0) {
board[idx][j] = board[i][j];
idx--;
}
}
for (int i = idx; i >= 0; i--) {
board[i][j] = 0;
}
}
}
return board;
}
}
Java solution
correspondant/originalpublic class Solution {
public int[][] candyCrush(int[][] board) {
int m = board.length;
int n = board[0].length;
boolean stable = false;
while (!stable) {
stable = true;
boolean[][] crush = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n - 2; j++) {
int v = Math.abs(board[i][j]);
if (v != 0 && v == Math.abs(board[i][j + 1]) && v == Math.abs(board[i][j + 2])) {
stable = false;
crush[i][j] = crush[i][j + 1] = crush[i][j + 2] = true;
}
}
}
for (int i = 0; i < m - 2; i++) {
for (int j = 0; j < n; j++) {
int v = Math.abs(board[i][j]);
if (v != 0 && v == Math.abs(board[i + 1][j]) && v == Math.abs(board[i + 2][j])) {
stable = false;
crush[i][j] = crush[i + 1][j] = crush[i + 2][j] = true;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (crush[i][j]) {
board[i][j] = 0;
}
}
}
for (int j = 0; j < n; j++) {
int idx = m - 1;
for (int i = m - 1; i >= 0; i--) {
if (board[i][j] != 0) {
board[idx][j] = board[i][j];
idx--;
}
}
for (int i = idx; i >= 0; i--) {
board[i][j] = 0;
}
}
}
return board;
}
}
JavaScript solution
correspondant/originalvar candyCrush = function(board) {
let m = board.length;
let n = board[0].length;
let stable = false;
while (!stable) {
stable = true;
let crush = Array.from({ length: m }, () => Array(n).fill(false));
for (let i = 0; i < m; i++) {
for (let j = 0; j < n - 2; j++) {
let v = Math.abs(board[i][j]);
if (v !== 0 && v === Math.abs(board[i][j + 1]) && v === Math.abs(board[i][j + 2])) {
stable = false;
crush[i][j] = crush[i][j + 1] = crush[i][j + 2] = true;
}
}
}
for (let i = 0; i < m - 2; i++) {
for (let j = 0; j < n; j++) {
let v = Math.abs(board[i][j]);
if (v !== 0 && v === Math.abs(board[i + 1][j]) && v === Math.abs(board[i + 2][j])) {
stable = false;
crush[i][j] = crush[i + 1][j] = crush[i + 2][j] = true;
}
}
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (crush[i][j]) {
board[i][j] = 0;
}
}
}
for (let j = 0; j < n; j++) {
let idx = m - 1;
for (let i = m - 1; i >= 0; i--) {
if (board[i][j] !== 0) {
board[idx][j] = board[i][j];
idx--;
}
}
for (let i = idx; i >= 0; i--) {
board[i][j] = 0;
}
}
}
return board;
};
Python solution
correspondant/originaldef candyCrush(board):
m, n = len(board), len(board[0])
stable = False
while not stable:
stable = True
crush = [[False] * n for _ in range(m)]
for i in range(m):
for j in range(n - 2):
if abs(board[i][j]) == abs(board[i][j + 1]) == abs(board[i][j + 2]) != 0:
stable = False
crush[i][j] = crush[i][j + 1] = crush[i][j + 2] = True
for i in range(m - 2):
for j in range(n):
if abs(board[i][j]) == abs(board[i + 1][j]) == abs(board[i + 2][j]) != 0:
stable = False
crush[i][j] = crush[i + 1][j] = crush[i + 2][j] = True
for i in range(m):
for j in range(n):
if crush[i][j]:
board[i][j] = 0
for j in range(n):
idx = m - 1
for i in range(m - 1, -1, -1):
if board[i][j] != 0:
board[idx][j] = board[i][j]
idx -= 1
for i in range(idx, -1, -1):
board[i][j] = 0
return board
Go solution
correspondant/originalpackage main
func candyCrush(board [][]int) [][]int {
m := len(board)
n := len(board[0])
stable := false
for !stable {
stable = true
crush := make([][]bool, m)
for i := range crush {
crush[i] = make([]bool, n)
}
for i := 0; i < m; i++ {
for j := 0; j < n-2; j++ {
if abs(board[i][j]) == abs(board[i][j+1]) && abs(board[i][j+1]) == abs(board[i][j+2]) && board[i][j] != 0 {
stable = false
crush[i][j] = true
crush[i][j+1] = true
crush[i][j+2] = true
}
}
}
for i := 0; i < m-2; i++ {
for j := 0; j < n; j++ {
if abs(board[i][j]) == abs(board[i+1][j]) && abs(board[i+1][j]) == abs(board[i+2][j]) && board[i][j] != 0 {
stable = false
crush[i][j] = true
crush[i+1][j] = true
crush[i+2][j] = true
}
}
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if crush[i][j] {
board[i][j] = 0
}
}
}
for j := 0; j < n; j++ {
idx := m - 1
for i := m - 1; i >= 0; i-- {
if board[i][j] != 0 {
board[idx][j] = board[i][j]
idx--
}
}
for i := idx; i >= 0; i-- {
board[i][j] = 0
}
}
}
return board
}
func abs(a int) int {
if a < 0 {
return -a
}
return a
}
Algorithm
Exemple:
Input: board = [[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]]
Output: [[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]]
👨💻
Algorithme:
find все группы из трех или более одинаковых конфет, как в горизонтальном, так и в вертикальном направлениях, и отметьте их для удаления.
Удалите отмеченные конфеты, установив их значение в 0.
Переместите конфеты вниз, чтобы заполнить пустые ячейки, и повторите процесс, пока не останется групп конфет для удаления.
😎
Vacancies for this task
offres actives with overlapping task tags are affichés.