Runtime error with my c code


  • 0
    X

    sometime it's runtime error
    sometimes it gets the wrong answer for the test case {"01","01"}
    but it works fine on my computer
    anyone can help me to check what's wrong with my code on lettcode OJ

    typedef struct pos_pair
    {
    int height;
    int left;
    }pos_pair;

    int max(int a, int b)
    {
    return (a) > (b) ? (a) : (b);
    }

    int maximalRectangle(char** matrix, int rows, int cols) {
    int *height = (int *)malloc((cols+10) *sizeof(int));
    pos_pair *pair = (pos_pair *)malloc((cols+10) * sizeof(pos_pair));
    int i, j, k;
    int top = -1;
    int cur_left = 0;
    int max_area = 0;

    for(i=0; i<cols; i++)
    	height[i] = 0;
    
    for(i=0; i<rows; i++)
    {
    	for(j=0; j<cols; j++)
    	{
    		if(matrix[i][j] == '1')
    			height[j] += 1;
    		else
    			height[j] = 0;
    	}
    
        for(k=0; k<=top; k++)
        {
    	    pair[k].height = 0;
        	pair[k].left = 0;
        }
    	top = 0;
    
    	for(j=0; j<=cols; j++)
    	{
    		if(height[j] > pair[top].height)
    		{
    			top++;
    			pair[top].height = height[j];
    			pair[top].left = j;
    		}
    		else
    		{
    			cur_left = j;
    			while(height[j] < pair[top].height)
    			{
    				cur_left = pair[top].left;
    				max_area = max(max_area, pair[top].height * (j - cur_left));
    				top--;
    			}
    			top++;
    			pair[top].height = height[j];
    			pair[top].left = cur_left;
    		}
    	}
    }
    free(pair);
    free(height);
    
    return max_area;
    

    }


Log in to reply
 

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