-
Notifications
You must be signed in to change notification settings - Fork 46
/
_723.py
54 lines (47 loc) · 1.52 KB
/
_723.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
LeetCode 723 - Candy Crush
Simulation Problem
Note that, it is not finding connected components of size >= 3.
It just finds horizontal or vertical connected strips of size >= 3.
"""
class Solution:
def candyCrush(self, board):
"""
:type board: List[List[int]]
:rtype: List[List[int]]
"""
r, c = (len(board), len(board[0]))
def drop(board):
changed = False
for j in range(c):
len = r
for i in reversed(range(r)):
if board[i][j] != 0:
board[len - 1][j] = board[i][j]
len -= 1
for i in range(len):
board[i][j] = 0
return changed
clear = []
for i in range(r):
j = 0
while j < c:
jj = j
while jj < c and board[i][j] == board[i][jj]:
jj += 1
if jj - j >= 3 and board[i][j] != 0:
clear += [(i, k) for k in range(j, jj)]
j = jj
for j in range(c):
i = 0
while i < r:
ii = i
while ii < r and board[i][j] == board[ii][j]:
ii += 1
if ii - i >= 3 and board[i][j] != 0:
clear += [(k, j) for k in range(i, ii)]
i = ii
for (i, j) in clear:
board[i][j] = 0
drop(board)
return self.candyCrush(board) if clear else board