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& 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]
  • Two Pointer Approach
class Solution {
public:
    int maxArea(vector& height) {
        int i = 0;
        int j = height.size() - 1;
        int maxArea = 0;
        int area = 0;
        while(i < j) {
            area = (j - i) * (height[i]

参考资料

  1. https://leetcode.com/problems/container-with-most-water/description/

你可能感兴趣的:(Leetcode 11. Container With Most Water)