LeetCode 42. Catching Rainwater

Topic 1

Given n n n non-negative integers representing the height of each column with a width of 1, calculate how much rain can be received after rain by the columns arranged according to this arrangement.

Above is an altitude map represented by an array [0,1,0,2,1,0,1,3,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]. In this case, six units of rainwater can be accepted (the blue part represents rainwater). Thank Marcos for contributing this map.

Example:

Input: [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1, 1]
Output: 6

Successful first submission (C)

int minn(int a,int b)
{
    if(a>b)
        return b;
    else
        return a;
}

int rongJi(int *height, int begin, int end)
{
    int hig = minn(height[begin],height[end]);
    int total = 0;
    for(int i = begin+1; i<end;i++)
    {
        total += (hig-height[i]);
    }
    return total;
}

int getMax(int *height, int begin, int end)
{
    int a = begin;
    for(int i = begin;i < end; i++)
    {
    	if(height[a]< height[i])
    		a=i;
    }
    return a;
}

int trap(int* height, int heightSize) 
{
    if(heightSize == 0 || heightSize == 1 || heightSize == 2)
        return 0;
    int total = 0;
    int left = getMax(height, 0, heightSize);
    int right = left;
    int gao;
    
    while(left != 0 || right != (heightSize-1))
    {
    	if(left != 0)
    	{
    		gao = getMax(height,0,left);
    		total += rongJi(height,gao,left);
    		//printf("%d + %d = %d\n==========\n",gao,left,rongJi(height,gao,left));
    		left = gao;
		}
		
		if(right != (heightSize-1))
		{
			gao = getMax(height,right+1,heightSize);
			total += rongJi(height,right,gao);
			//printf("%d + %d = %d\n==========\n",right,gao,rongJi(height,right,gao));
			right = gao;
		}
    }
    return total;
}

Summary of this submission:

1. The reason why the submission has not been passed is that the boundary value is not taken into account. In the future brush questions, it is necessary to judge the situation where the input is empty and equal boundary value.

2. The specific process is as follows, the writing is not very clear, and it will be changed later.

3. The results are as follows:

3. Analysis and summary of other people's high-quality code

1. Code first

int* QuickSort(int* nums, int *seq, int start, int end);
int trap(int* height, int heightSize) {
    int *sortheight = (int*)malloc(sizeof(int) * heightSize);
    int *seq = (int*)malloc(sizeof(int) * heightSize);
    for(int i = 0; i < heightSize; i++)
    {
        seq[i] = i;
        sortheight[i] = height[i];
    }      
    QuickSort(sortheight, seq, 0, heightSize - 1);
    int left = seq[heightSize - 1], right = left, result = 0;
    for(int i = heightSize - 1; i >= 0; i--)
    {
        //Calculate the pit between seq[i] and left or right, regardless of the middle
        if(seq[i] < left)
        {
            for(int j = seq[i] + 1; j <left; j++)
                result += height[seq[i]] - height[j];
            left = seq[i];
        }
        else if(seq[i] > right)
        {
             for(int j = right + 1; j <seq[i]; j++)
                result += height[seq[i]] - height[j];
            right = seq[i];
        }
    }
    return result;
}

int* QuickSort(int* nums, int *seq, int start, int end)
{
    if(start>=end)return;
    int i = start, j = end, temp = nums[start], tempseq = seq[start];
    while(i<j)
    {
        while(nums[j]>=temp && i<j) j--;
        nums[i] = nums[j]; seq[i] = seq[j];
        while(nums[i]<=temp && i<j) i++;
        nums[j] = nums[i]; seq[j] = seq[i];
    }
    nums[i] = temp; seq[i] = tempseq;
    QuickSort(nums, seq, start, i-1);
    QuickSort(nums, seq, i+1, end);
    return;
}

 

Posted by davidp on Sun, 24 Mar 2019 10:18:27 -0700