42. Trapping Rain Water
2016-11-29 16:32
274 查看
题目:Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given
思路:
(1)用数组存储所有凸峰的下标。凸峰就是左右低,中间高;注意最前面和最后面的凸峰的处理;
(2)对于凸峰数组,firstSub初始化为第一个凸峰,把当前凸峰的峰值大于等于firstSub位置凸峰值,处理如下,从firstSub+1位置开始到当前凸峰的位置,如果当前的列值小于最小值(firstSub所在凸峰的值),则最小值-当前列值存储到sum中,同时把当前凸峰标记为firstSub;否则跳过。
如果下一个凸峰的峰值小于前一个凸峰的值,则继续探索下一个凸峰。
(3)如果firstSub表示的凸峰不是最后一个凸峰,表示还有没有计算的值。
此时,firstSub后面的凸峰值都是小于firstSub所在凸峰的峰值的。
//剩余的凸峰都没有firstSub所在的凸峰高
//找到剩余元素中的最大的元素,然后对数据进行相加
//如果是最后一个凸峰,则结束,否则继续找剩余的最大元素
代码:
int trap(int* height, int heightSize) {
if(heightSize==1 || heightSize==0) return 0;
int* tmp=(int*)malloc(sizeof(int)*heightSize);
memset(tmp,0,sizeof(int)*heightSize);
int sub=-1;
int pre=height[0];
int mid=height[1];
int post=height[2];
int i=0;
int j=0;
int k=0;
int sum=0;
int firstHeight=0;
int firstSub=0;
printf("The heightSize is %d\n",heightSize);
for(i=0;i<heightSize-1;i++)
{
pre=height[i];
mid=height[i+1];
if(i != (heightSize-2) )
post=height[i+2];
//处理第一次
if(i==0 && pre >0 && mid<=pre )
{
printf("The sub is %d\n",i);
tmp[++sub]=i;
}
if(i==(heightSize-2) && pre <= mid)
{
printf("The sub is %d\n",i+1);
tmp[++sub]=i+1;
continue;
}
if(pre <= mid && mid >= post )
{
printf("The sub is %d\n",i+1);
tmp[++sub]=i+1;
}
}
//后一个凸峰比前一个凸峰高,就把数据相加
for(i=0;i<=sub;i++)
{
if(i==0)
{
firstSub=i;
firstHeight=tmp[i];
}
else if(height[tmp[i]] >= height[firstHeight] )
{
for(j=firstHeight+1; j < tmp[i];j++)
{
if(height[firstHeight] > height[j])
sum+=(height[firstHeight]-height[j]);
}
firstSub=i;
firstHeight=tmp[i];
}
}
//剩余的凸峰都没有firstSub所在的凸峰高
//找到剩余元素中的最大的元素,然后对数据进行相加
//如果是最后一个凸峰,则结束,否则继续找剩余的最大元素
while(firstSub!=sub)
{
int m=height[tmp[firstSub+1]];
int m_sub=firstSub+1;
for(k=firstSub+1; k<=sub; k++)
{
if(m < height[tmp[k]] )
{
m=height[tmp[k]];
m_sub=k;
}
}
int max=height[tmp[firstSub]];
int min=height[tmp[m_sub]];
for(j=tmp[firstSub]+1; j < tmp[m_sub];j++)
{
if(min > height[j])
sum+=(min-height[j]);
}
firstSub=m_sub;
}
return sum;
}
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given
[0,1,0,2,1,0,1,3,2,1,2,1], return
6.
思路:
(1)用数组存储所有凸峰的下标。凸峰就是左右低,中间高;注意最前面和最后面的凸峰的处理;
(2)对于凸峰数组,firstSub初始化为第一个凸峰,把当前凸峰的峰值大于等于firstSub位置凸峰值,处理如下,从firstSub+1位置开始到当前凸峰的位置,如果当前的列值小于最小值(firstSub所在凸峰的值),则最小值-当前列值存储到sum中,同时把当前凸峰标记为firstSub;否则跳过。
如果下一个凸峰的峰值小于前一个凸峰的值,则继续探索下一个凸峰。
(3)如果firstSub表示的凸峰不是最后一个凸峰,表示还有没有计算的值。
此时,firstSub后面的凸峰值都是小于firstSub所在凸峰的峰值的。
//剩余的凸峰都没有firstSub所在的凸峰高
//找到剩余元素中的最大的元素,然后对数据进行相加
//如果是最后一个凸峰,则结束,否则继续找剩余的最大元素
代码:
int trap(int* height, int heightSize) {
if(heightSize==1 || heightSize==0) return 0;
int* tmp=(int*)malloc(sizeof(int)*heightSize);
memset(tmp,0,sizeof(int)*heightSize);
int sub=-1;
int pre=height[0];
int mid=height[1];
int post=height[2];
int i=0;
int j=0;
int k=0;
int sum=0;
int firstHeight=0;
int firstSub=0;
printf("The heightSize is %d\n",heightSize);
for(i=0;i<heightSize-1;i++)
{
pre=height[i];
mid=height[i+1];
if(i != (heightSize-2) )
post=height[i+2];
//处理第一次
if(i==0 && pre >0 && mid<=pre )
{
printf("The sub is %d\n",i);
tmp[++sub]=i;
}
if(i==(heightSize-2) && pre <= mid)
{
printf("The sub is %d\n",i+1);
tmp[++sub]=i+1;
continue;
}
if(pre <= mid && mid >= post )
{
printf("The sub is %d\n",i+1);
tmp[++sub]=i+1;
}
}
//后一个凸峰比前一个凸峰高,就把数据相加
for(i=0;i<=sub;i++)
{
if(i==0)
{
firstSub=i;
firstHeight=tmp[i];
}
else if(height[tmp[i]] >= height[firstHeight] )
{
for(j=firstHeight+1; j < tmp[i];j++)
{
if(height[firstHeight] > height[j])
sum+=(height[firstHeight]-height[j]);
}
firstSub=i;
firstHeight=tmp[i];
}
}
//剩余的凸峰都没有firstSub所在的凸峰高
//找到剩余元素中的最大的元素,然后对数据进行相加
//如果是最后一个凸峰,则结束,否则继续找剩余的最大元素
while(firstSub!=sub)
{
int m=height[tmp[firstSub+1]];
int m_sub=firstSub+1;
for(k=firstSub+1; k<=sub; k++)
{
if(m < height[tmp[k]] )
{
m=height[tmp[k]];
m_sub=k;
}
}
int max=height[tmp[firstSub]];
int min=height[tmp[m_sub]];
for(j=tmp[firstSub]+1; j < tmp[m_sub];j++)
{
if(min > height[j])
sum+=(min-height[j]);
}
firstSub=m_sub;
}
return sum;
}
相关文章推荐
- 42. Trapping Rain Water
- LeetCode 42 Trapping Rain Water(积水体积)
- Leetcode-42 Trapping in Rain Water
- 42-Trapping Rain Water
- 42. Trapping Rain Water
- 42. Trapping Rain Water
- LeetCode(42) Trapping Rain Water
- 42. Trapping Rain Water
- LeetCode(42)Trapping Rain Water
- LeetCode42 Trapping Rain Water
- [LeetCode]42 Trapping Rain Water
- LeetCode(42)Trapping Rain Water
- LeetCode (42): Trapping Rain Water
- LeetCode-42:Trapping Rain Water
- [leetcode-42]Trapping Rain Water(java)
- Leetcode42 Trapping Rain Water
- [leetcode] 42 Trapping Rain Water
- 42. Trapping Rain Water
- Leet Code 42 Trapping Rain Water - Java
- 42. Trapping Rain Water