Find all the values to be deleted and make them negative and then remove all the negative values from the columns one by one
Time Complexity:O(M*N*C) where C is the number of crushing rounds req
Space Complexity:O(M*N)
Approach 2: Rotate array to ease gravity
classSolution(object):defcandyCrush(self,board):""" :type board: List[List[int]] :rtype: List[List[int]] """ board =map(list,zip(*reversed(board))) R, C =len(board),len(board[0])whileTrue: remove =set()for i inrange(R):for j inrange(2, C):if board[i][j] !=0and board[i][j] == board[i][j-1] == board[i][j-2]: remove |={(i,j), (i,j-1), (i,j-2)}for j inrange(C):for i inrange(2, R):if board[i][j] !=0and board[i][j] == board[i-1][j] == board[i-2][j]: remove |={(i,j), (i-1,j), (i-2,j)}if remove:for i,j in remove: board[i][j] =0for i inrange(R): count =0 board[i]= [x for x in board[i]if x] board[i].extend([0]*(C -len(board[i])))else:breakreturnlist(reversed(map(list,zip(*board))))
After iterating over all rows and columns each time to find a continuous block of candies to remove, we make them zero. Now instead of having a "gravity" function what made sense was to actually rotate the array counter clockwise and shift each of the items in the row left instead of down in a column.
Time Complexity:O(M*N*C) where C is the number of crushing rounds req