Leetcode 11. Container With Most Water

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Container With Most Water

2. Solution

  • Brute Force
class Solution {
public:
    int maxArea(vector<int>& height) {
        int length = height.size();
        int maxArea = 0;
        int area = 0;
        for(int i = 0; i < length; i++) {
            for(int j = i + 1; j < length; j++) {
                area = (j - i) * (height[i]<height[j]?height[i]:height[j]);
                if(maxArea < area) {
                    maxArea = area;
                }
            }
        }
        return maxArea;
    }
};
  • Two Pointer Approach
class Solution {
public:
    int maxArea(vector<int>& height) {
        int i = 0;
        int j = height.size() - 1;
        int maxArea = 0;
        int area = 0;
        while(i < j) {
            area = (j - i) * (height[i]<height[j]?height[i]:height[j]);
            if(maxArea < area) {
                maxArea = area;
            }
            height[i]<height[j]?i++:j--;
        }
        return maxArea;
    }
};

参考资料

  1. https://leetcode.com/problems/container-with-most-water/description/
阅读更多

扫码向博主提问

SnailTyan

博客专家

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • 深度学习
  • PyTorch
  • OCR
  • Docker
  • Caffe
去开通我的Chat快问
版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/80924654
文章标签: Leetcode
个人分类: Algorithm
所属专栏: Leetcode
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭