zoukankan      html  css  js  c++  java
  • 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了

    2015/03/29:

    class Solution {
    public:
        int maxArea(vector<int> &height) {
            int l = 0, r = height.size()-1, water = 0;
            while(l < r){
                water = max(water, (r - l) * (height[l] > height[r] ? height[r--] : height[l++]));
            }
            return water;
        }
    };

    python:

    class Solution:
        # @return an integer
        def maxArea(self, height):
            water, l, r = 0, 0, len(height)-1
            while l < r:
                water = max(water, (r - l)*min(height[l], height[r]))
                if height[l] < height[r]:
                    l += 1
                else:
                    r -= 1
            return water
  • 相关阅读:
    JS实现菜单滚动到一定高度后固定
    原生js如何获取某一元素的高度
    在Vue项目中,添加的背景图片在服务器上不显示,如何处理
    单行文本截断 text-overflow
    使用line-height垂直居中在安卓手机上效果不好
    css 苹方字体
    十二. for of 示例 (可以解决大多数应用场景)
    npm详解
    webpack搭建服务器,随时修改刷新
    探讨弹性布局Flexible Box
  • 原文地址:https://www.cnblogs.com/higerzhang/p/4025475.html
Copyright © 2011-2022 走看看