LeetCode 11 Container With Most Water

题目如下:

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container.

翻译略去,直接说思路,刚开始我想错了,直接想成了计算题型面积(a[j]-a[i])*(j-i)/2,而且还用了个穷举法。

这里应该计算并比较min(h[i], h[j]) * (j - i)。

选择从两端到中间递归查找,程序如下:

class Solution {
public:
   int maxArea(vector<int> &height)
{
    int ret = findMax(height, 0, height.size()-1, 0);
    return ret;
}
int findMax(vector<int> &h, int i , int j , int area)
{
    int temp = 0;

    if(i == j)
    {
        cout << i << " " << j << endl;
        return area;
    }

    temp = (h[i]>h[j]?h[j]:h[i]) * (j - i) ;
    if(h[i] > h[j])
    {
        j -= 1;
    }
    else
    {
        i += 1;
    }
    if(temp < area)
        findMax(h, i, j , area);
    else
        findMax(h, i, j, temp);
}
};

非递归版本:

class Solution {
public:
   int maxArea(vector<int> &height)
{
    int area = 0;
    int iStart = 0;
    int iEnd = height.size()-1;
    vector<int> h = height;

    while(iStart != iEnd)
    {
        int temp = (h[iStart] > h[iEnd]?h[iEnd]:h[iStart]) * (iEnd - iStart);
        if(temp > area)
        {
            area = temp;
        }
        if(h[iStart] > h[iEnd])
        {
            iEnd -= 1;
        }
        else
        {
            iStart += 1;
        }
    }
    return area;
}
};

加油!

时间: 2024-10-07 15:04:50

LeetCode 11 Container With Most Water的相关文章

Leetcode 11. Container With Most Water (two pointers)

Leetcode: 11 there are two ways to deal with two pointers one is O(n), two pointers moves from both side Another is O(2N), two pointer move from the same side Idea for this, choose the first one and then if there is a smaller one, change that corresp

LeetCode 11. Container With Most Water (装最多水的容器)

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

LeetCode 11 Container With Most Water(最大水容器)

翻译 给定n个非负整数a1,a2,...,an,其中每个代表一个点坐标(i,ai). n个垂直线段例如线段的两个端点在(i,ai)和(i,0). 找到两个线段,与x轴形成一个容器,使其包含最多的水. 备注:你不必倾倒容器. 翻译 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such tha

LeetCode 11 Container With Most Water (C,C++,Java,Python)

Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a

[LeetCode] 11. Container With Most Water My Submissions Question 解题思路

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

[LeetCode] 11. Container With Most Water 装最多水的容器

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

[leetcode]11. Container With Most Water存水最多的容器

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a contain

LeetCode 11 Container With Most Water 装尽可能多的水

题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a cont

[LeetCode]11. Container With Most Water 盛最多水的容器

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a contain