C++ Brute Solution


  • 0
    Y
    class Solution {
    public:
    	vector<vector<int>> candyCrush(vector<vector<int>>& board) {
    		if (board.size() == 0 || board[0].size() == 0)
    		{
    			return board;
    		}
    
    		int m = board.size();
    		int n = board[0].size();
    
    		int cleanCount = 1;
    		while (cleanCount > 0)
    		{
    			//mark
    			for (int i = 0; i < m; i++)
    			{
    				int k = 0;
    				for (int j = 0; j <= n; j++)
    				{
    					if (j == n || abs(board[i][j]) != abs(board[i][k]))
    					{
    						if (j - k >= 3)
    						{
    							for (int l = k; l < j; l++)
    							{
    								board[i][l] = -abs(board[i][l]);
    							}
    						}
    
    						k = j;
    					}
    				}
    			}
    
    			for (int j = 0; j < n; j++)
    			{
    				int k = 0;
    				for (int i = 0; i <= m; i++)
    				{
    					if (i == m || abs(board[i][j]) != abs(board[k][j]))
    					{
    						if (i - k >= 3)
    						{
    							for (int l = k; l < i; l++)
    							{
    								board[l][j] = -abs(board[l][j]);
    							}
    						}
    
    						k = i;
    					}
    				}
    			}
    
    			//clean
    			cleanCount = 0;
    			for (int j = 0; j < n; j++)
    			{
    				int k = m - 1;
    				for (int i = m - 1; i >= 0; i--)
    				{
    					if (board[i][j] > 0)
    					{
    						board[k--][j] = board[i][j];
    					}
    					else if (board[i][j] < 0)
    					{
    						cleanCount++;
    					}
    				}
    
    				for (int i = k; i >= 0; i--)
    				{
    					board[i][j] = 0;
    				}
    			}
    		}
    
    		return board;
    	}
    };
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.