leetcode第11题--Container With Most Water

Problem:

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.

题目的意思是,在(x,y)坐标中,每个点做与x轴垂直的直线后,求哪两根直线和x轴所能装的水最多,不能倾斜的意思就是不是指梯形的面积,而是短板效应的矩形面积。先暴力试了下,练了下手感。不出所料N方的超时。

class Solution {
public:
int maxArea(vector<int> &height)
{
    int area = 0, temparea;
    int *temp = new int[height.size()];
    for (int i = 0; i < height.size(); i++)
    {
        int maxN = 0;
        for (int j = 0; j < height.size(); j++)
        {
            if (i != j)
            {
                temparea = (height[i]<height[j]?height[i]:height[j]) * abs(j - i);
                if (temparea > maxN)
                    maxN = temparea;
            }
        }
        temp[i] = maxN;
    }
    for (int i = 0; i < height.size(); i++)
    {
        if (temp[i] > area)
            area = temp[i];
    }
    delete[] temp;
    return area;
}
};

后来还想,能不能排序之后再判断,发现sort又是不稳定的所以就放弃了。后来发现可以从两边往里收缩的办法解决。两边往里的还有第一题Two Sum也是这样做的。

为什么用两边往里呢,因为我们我们要的面积是两条直线的距离*两条直线短的那条的值,所以,我们先定一个,距离最大就是头和尾了,如果比这个距离小的,又还想比我现在大的话,那就只有高增加才有可能,这个时候就是把短的那条对应的位置往前看一位,看看可不可能有比短的长,且乘出来的面积是比之前大的,如果大,那就记录下来。为什么要用短的那边往里看呢,因为如果长的往里的话,就是下去一根再长也是根据短板效应看短的。根据这个思路自己整理了下代码如下:

class Solution {
public:
int maxArea(vector<int> &height)
{
    int left = 0, right = height.size() - 1;
    int maxA = 0;
    while(left < right)
    {
        if (height[left] < height[right])
        {
            int tmp = (right - left) * height[left];
            left++;
            if (tmp > maxA)
                maxA = tmp;
        }
        else
        {
            int tmp = (right - left) * height[right];
            if (tmp > maxA)
                maxA = tmp;
            right--;
        }
    }
    return maxA;
}
};

这样就Accept了

时间: 2024-10-01 07:13:01

leetcode第11题--Container With Most Water的相关文章

leetcode_11题——Container With Most Water(两个指针)

Container With Most Water Total Accepted: 38917 Total Submissions: 121822My Submissions Question Solution 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

LeetCode(11)题解: Container With Most Water

https://leetcode.com/problems/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)

LeetCode第11题 盛水最多的容器

/* 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) . 在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0). 找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水. 说明:你不能倾斜容器,且 n 的值至少为 2.*/ 思路: 类似于木桶效应. 每次移动较短的线,保持长线不动.(如果短线不动而长线动,容量减小(y没有增大反而x变小)) 记录最大值. 1 class Solution11 { 2 3 pu

LeetCode算法题python解法:11. Container With Most Water

LeetCode第十一题 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 fo

11. Container With Most Water【leetcode】,java算法,数组,求最大水池注水量

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 togethe

刷题11. Container With Most Water

一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<iostream> #include<vector> #include<math.h> using namespace std; class Solution{ public: int maxArea(vector<int> &height){ int max =

[LeetCode][JavaScript]Container With Most Water

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 wi

LeetCode: 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

Two Points问题--之LeetCode 11题

---恢复内容开始--- Container with most  water--LeetCode11题 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 t