LeedCode_ rainwater

LeedCode_ rainwater

Topic Description

Given n each represents a non-negative integer of 1 column width height map calculated Click column arrangement, then able to take much rain rain.
Here Insert Picture Description

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

Links: https://leetcode-cn.com/problems/trapping-rain-water
Reference article: https://blog.csdn.net/kexuanxiu1163/article/details/100681468
key ideas: storage capacity in one location = min (maximum left and right maximum) -height

int trap(vector<int>& height) 
{
	int n = height.size();
	if (n == 0)
	{
		return 0;
	}
	vector<int> right(n,0);
	vector<int> left(n,0);
	for (int i = 1; i < n-1; i++)
	{
		left[i] = max(height[i-1], left[i - 1]);
		right[n - 1 - i] = max(height[n - i], right[n - i]);
	}
	int ans=0;
	int temp;
	for (int i = 0; i < n; i++)
	{
		temp = min(left[i], right[i]) - height[i];
		if (temp < 0)
			continue;
		ans += temp;
	}
	return ans;
}
Published 26 original articles · won praise 6 · views 10000 +

Guess you like

Origin blog.csdn.net/luncy_yuan/article/details/104069130