Can anyone help me to figure out what is wrong with my code? Test case [[1]] failed.


  • 0
    C
    int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridRowSize, int obstacleGridColSize) {
    
    int max = 0;
    int row = 0;
    int col = 0;
    int *maxPath = malloc(obstacleGridRowSize * obstacleGridColSize * sizeof(int));
    
    for (row = 0; row < obstacleGridRowSize; row++)
    {
    	for (col = 0; col < obstacleGridColSize; col++)
    	{
    		if (*((int *)obstacleGrid + row * obstacleGridColSize + col) == 1)
    		{
    			max = maxPath[row * obstacleGridColSize + col] = 0;
    		}
    		else
    		{
    			if (row > 0 && col > 0)
    			{
    				max = maxPath[row * obstacleGridColSize + col] = maxPath[(row - 1) * obstacleGridColSize + col] 
    													   + maxPath[row * obstacleGridColSize + col - 1];
    			}
    			else if (row > 0)
    			{
    				max = maxPath[row * obstacleGridColSize + col] = maxPath[(row - 1) * obstacleGridColSize + col];
    			}
    			else if (col > 0)
    			{
    				max = maxPath[row * obstacleGridColSize + col] = maxPath[row * obstacleGridColSize + col - 1];	
    			}
    			else
    			{
    				max = maxPath[row * obstacleGridColSize + col] = 1;
    			}
    		}
    
    	}
    }
    
    free(maxPath);
    
    return max;
    

    }


Log in to reply
 

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