【LeetCode】【Python题解】Container with most water

这道题真真是在面试中碰到过,可惜当时复杂度到O(n2)了,太挫了,怪不得没有通过面试。

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.

题目说的有点复杂,大意是利用x轴作底,两个任意的竖直线段作杯壁,何时盛水最多。

木桶原理大家肯定都知道,水盛多少取决于最短的杯壁,所以此题还可以引申为往围成的区域内放矩形,怎样使得矩形面积最大。题目中的不能倾斜(slant:倾斜,倾倒)对应为矩形必须水平放置。

复杂度为O(n)的思想是贪心原理,先从底边最大的情况考虑,计算最大面积后,此时要将底边长度减1,只需要将杯壁较短的那一边移动一个单位距离,得到的解必定优于杯壁较长那边移动的情况。这样保证每次移动都得到的是局部最优解。

class Solution {
public:
    int maxArea(vector<int> &height) {
        int Len = height.size(),low=0,high=Len-1;
        int maxV = 0;
        while(low<high){
            maxV = max(maxV,(high-low)*min(height[low],height[high]));
            if (height[low]<height[high]) low++;
            else high--;
        }
        return maxV;
    }
};
时间: 2024-12-20 12:32:47

【LeetCode】【Python题解】Container with most water的相关文章

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.ZigZag Conversion ,Container With Most Water

ZigZag Conversion: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by lin

leetcode题解||Container With Most Water问题

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】011 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 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51820937 Subject 出处: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

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

【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题目: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 problem 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 Array 11 Container With Most Water

public class Solution { public int maxArea(int[] height) { int maxa = 0; int maxi = 0; if(height.length<2) return 0; for(int i=0;i<height.length-1;i++){ if(height[i]<height[maxi]){ continue; } for(int j=height.length-1;j>i;j--){ if(height[j]&g