leetcode桶装水问题

妖狐艹你老母 2022-06-06 04:13 262阅读 0赞
  1. class Solution {
  2. public:
  3. int maxArea(vector<int>& height) {
  4. int head=0;
  5. int maxV=0,temp=0;
  6. int end=height.size()-1; //记录头尾
  7. while(head<end)
  8. {
  9. temp=min(height[head],height[end])*(end-head); //面积取决于短板和底的长度
  10. maxV=max(temp,maxV); //记录临时变量与之前最大值相比取大值
  11. if(height[head]<height[end]) //**重点***谁小谁往里缩,不可能错过最大值
  12. head++;
  13. else
  14. end--;
  15. }
  16. return maxV;
  17. }
  18. };

发表评论

表情:
评论列表 (有 0 条评论,262人围观)

还没有评论,来说两句吧...

相关阅读

    相关 leetcode题->两数之和

    给定一个整数数组 `nums` 和一个目标值 `target`,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是