LeetCode—Trapping Rain Water 储水问题,从两边向中间

该博客介绍了如何解决LeetCode上的Trapping Rain Water问题,提出从两边向中间的策略来简化计算。通过保持左右两侧的高度次高值,并逐步向中间推进,可以有效地计算出能够储存的雨水量。对于某些复杂情况,如9 6 8 8 5 6 3 6,博主提到这种策略可能不适用,并表示会后续补充完整解法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

题目倒是挺简单的看着,然后开始做的时候发现最大的问题就是不知道最高点和次高点的位置

如果直接从左向右计算,那么存在的情况十分多

如果换个思路,从两边向中间,那么题目就简单很多了保存一个次高位的数据,如果有不理解,自己画个图,就很好理解了。


class Solution {
public:
    int trap(int A[], int n) {
       if(A == NULL || n == 0)
       {
           return 0;
       }
       int nextMax = 0;
       int left = 0;
       int right = n-1;
       int res = 0;
      while(left < right)
      {
          if(A[left] < A[right])
          {
              nextMax = max(A[left],nextMax);
              res += nextMax-A[left++];
          }
          else
          {
              nextMax = max(A[right],nextMax);
              res += nextMax-A[right--];
          }
      }
       return res;
    }
};


刚说到从左向右的计算方法,下面是自己写的,但是在一些情况还不太对

主要的思路就是如果一个阶段,最左边和最右边将中间的值包围起来,那么就可以利用这种方式计算

但是如果不是这种情况,比如 9 6 8  8 5 6 3 6

这种情况就很困难,之后回来将这种方法补全,不推荐

#include<iostream>
#include<vector>
using namespace std;
    int CalRain(vector<int> &temp)
    {
        int len = temp.size();
        if(len == 0 || len == 1)
        {
            return 0;
        }
        int res = 0;
        int max = temp[0];
        int nextmax = 0;
        int maxIndext = 0;
        int nextIndxt = 1;
        bool flag = 0;
        for(int i = 2; i < len; i++)
        {
            if(temp[i] >temp[i-1])  
			{
				flag = 1;//<说明有储水
				nextmax = temp[i];
				nextIndxt = i;
			}
			if(flag)
			{
				if(temp[i] > nextmax)
				{
					nextmax = temp[i];
					nextIndxt = i;
				}
			}
        }
        if(!flag) return 0;
        nextmax = (nextmax > max)?max:nextmax;
        for(int i = 1; i < nextIndxt; i++)
        {
			if((nextmax-temp[i]) > 0)
            res += (nextmax-temp[i]);
        }
        return res;
    }
int trap(int A[], int n) {
       if(A == NULL || n == 0)
       {
           return 0;
       }
       vector<int> temp;
       int max = A[0];
       int res = 0;
       temp.push_back(A[0]);
       for(int i = 1; i < (n-1); i++)
       {
           temp.push_back(A[i]);
           if(A[i] >= max && A[i+1] < A[i])//<可以以此条件判断
           {
               res += CalRain(temp);
               temp.clear();
               temp.push_back(A[i]);//<下一个模块还可以用
               max = A[i];
           }
           if(A[i] > max)
           {
               max = A[i];
           }
       }
       if(!temp.empty())
       {
		   temp.push_back(A[n-1]);
           res += CalRain(temp);
       }
       return res;
    }

int main()
{
	int A[7] = {9,6,8,8,5,6,3};
	int res = trap(A,7);
	int i = res;
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值