Leetcode11 Container With More Water && Leetcode42 Trapping Rain Water

这两个问题的解决方案都是基于头尾指针进行的,知识点在于对数组的处理。

Container With More Water

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

解法1:

从左到右遍历数组,记录最大的数字作为maxHeight(i处的高度),同时遍历i后的每一个数字,不断更新最大容量值,这种情况下的时间复杂度达到了O(n^2);  剪枝:当i右侧的高度小于i的高度时可直接跳过;此方法在leetcode平台所用时间为296ms,容易理解,但是在时间方面不是很理想。

class Solution {
public:
    int maxArea(vector<int>& height) {
        int maxHeight = height[0];
        int maxErea = 0;
        int maxIndex = 0;
        int conHeight,erea;
        int len = height.size();
        int i;
        for(i = 0 ; i < len; i++)
        {
            if(height[i] > maxHeight || i == 0)           //剪枝
            {
                maxHeight = height[i];
                int subMaxErea = 0;
                for(int j = i + 1;  j < len; j ++)
                {
                    conHeight= height[j] > height[i] ? height[i] : height[j];
                    int erea = conHeight * (j - i);
                    subMaxErea = subMaxErea > erea ? subMaxErea : erea;
                }
                if(subMaxErea > maxErea)
                {
                    maxErea = subMaxErea;
                }
            }
          
        }
         return maxErea;
    }
};

解法2:

采用左右指针同时移动的方式,大大降低了时间复杂度,为O(n),leetcode运行时间为16ms

这种做法一直比较困扰我的点在于当height[aLeft] < height[aRight]时对left的值进行更新,后来发现原因在于左右更新的终点在中间最大的那个数(这样才能保证在最短时间内容器获得最大容量),因此left,right两个指针向最大值的方向靠拢;(这是本题需要学的知识点:左右更新指针,使其到达某个点以满足需求);

class Solution {
public:
    int maxArea(vector<int>& height) {
        
        int aLeft = 0;
        int aRight = height.size() - 1;
        
        int aMaxArea = 0;
        int aArea = 0;
        
        int aStartHeight = 0;
        
        while (aLeft < aRight) {
            aArea = (aRight - aLeft) * min(height[aLeft], height[aRight]);
            if (aArea > aMaxArea) { aMaxArea = aArea; }
            //计算,并按需更新容器容量
            if (height[aLeft] < height[aRight]) {     //左值小,更新左值
                aStartHeight = height[aLeft];
                while (height[aLeft] <= aStartHeight && aLeft < aRight) {
                    aLeft += 1;
                }
            } else {                                 //右值小,更新右值          
                aStartHeight = height[aRight];                 
                while (height[aRight] <= aStartHeight && aLeft < aRight) {
                    aRight -= 1;
                }
            }
        }
        return aMaxArea;
    }
};

Trapping Rain Water

题意: 计算图中蓝色部分的储水体积

Example:

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

这道题的思路与上面那道题类似,但是增加了一定的难度,此题不仅要记录数值的最大值,还要记录之前数值的值来计算储水体积;

(按理来说我认为这个题也可以从左到右的方式进行,但是由于算法不够精简,导致runtime error)

同样使用左右两个指针逼近中间最大值的方法,过程中记录储水体积

粗略判断此题的复杂度应该也是O(n),leetcode上跑的时间为4ms

class Solution {
public:
   int trap(vector<int>& height) {
       int left = 0;
       int right = height.size() - 1;
       int left_max = 0, right_max = 0;
       int water = 0;
       while(left <= right)
       {
           if(height[left] < height[right])
           {
               if(height[left] > left_max)
               {
                   left_max = height[left];
               }
               else
               {
                   water += left_max - height[left];
               }
               left ++;
           }
           else
           {
               if(height[right] > right_max)
               {
                   right_max = height[right];
               }
               else
               {
                   water += right_max - height[right];
               }
               right --;
           }
       }
       return water;
   }
   
};

这个题还有一种精简的解法,我觉得会比较难理解一些,算法和时间上和以上解法相差不多

class Solution {
public:
   int trap(vector<int>& height) {
       int l = 0, r = height.size()-1, level = 0, water = 0;
    while (l < r) {
        int lower = height[height[l] <= height[r] ? l++ : r--];
        level = max(level, lower);
        water += level - lower;
    }
    return water;
   }
};

思考:这两道题用的其实是一种思路,只是处理方式略微不同,但是在看完第一题的解法之后没有完全掌握,做第二题还是很吃力的一种状态,做题过程一定要思维活跃,活学活用。

技巧点:对于这种数组的处理问题,除了dp, KMP等技巧算法之外,正常的思维模式通常倾向于从左到右遍历数组,这种算法一般情况下也可以解出正确答案,但是有些问题比较绕,就要尽可能地去剪枝,否则就会出现超时等问题,思路反而也会被搅乱;此时就需要考虑一下设置左右两个指针去处理问题;甚至可以考虑从一个数组的中间出发(参考求字符串中的最长回文子串长度)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值