跳到主要内容

Candy Crush

描述

TODO

分析

TODO

代码

# Candy Crush
# Time complexity: O((R*C)^2)
# Space complexity: O(1)
def candyCrush(board):
R, C = len(board), len(board[0])
todo = False

for r in range(R):
for c in range(C-2):
v = abs(board[r][c])
if v != 0 and v == abs(board[r][c+1]) and v == abs(board[r][c+2]):
board[r][c] = board[r][c+1] = board[r][c+2] = -v
todo = True

for r in range(R-2):
for c in range(C):
v = abs(board[r][c])
if v != 0 and v == abs(board[r+1][c]) and v == abs(board[r+2][c]):
board[r][c] = board[r+1][c] = board[r+2][c] = -v
todo = True

# gravity
for c in range(C):
wr = R - 1
for r in range(R-1, -1, -1):
if board[r][c] > 0:
board[wr][c] = board[r][c]
wr -= 1
while wr >= 0:
board[wr][c] = 0
wr -= 1

return candyCrush(board) if todo else board