Leetcode#11Container With Most Water

Container With Most Water

Total Accepted: 38727 Total Submissions: 121231My Submissions

Question Solution

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.

public class Solution {

int max(int a, int b){

if(a>b)

return a;

else

return b;

}

public int maxArea(int[] height) {

int right = height.length-1, left =0;

int res = 0;

while(left < right)

{

res = max(res, (right-left)*(height[right]<height[left]?height[right--]:height[left++]));

}

return res;

}

}

时间: 2024-10-26 12:48:40

Leetcode#11Container With Most Water的相关文章

[leetcode]_Container With Most Water

题目:在二维坐标系下,有很多个挡板,有两个挡板之间能够积蓄的水的最大面积.如下图所示: 思路:我只想到暴力解法,用O(n2)的时间复杂度算出任意两个挡板形成的面积,这必须的过不了. 优化解法:O(n). 用两个指针 i 和 j 指向整个height[]数组的头尾. if i 指向的高度 < j 指向的高度 , then 用 i 指向的高度 * i , j之间的距离 求的面积,i 指针向后移. (i指针向后移的原因:因为当前的面积由i 的高度决定,相当于现在获得以i指针为一边挡板的最大面积,因为随

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

LeetCode &quot;Container With Most Water&quot; - GREEDY?

O(n^2) is a naive solution. As rule of thumb, there must be O(n) soluion. Yes - Greedy. We assume widest container contains the most water, greedily, but it is possible that we have higher heights with narrower range, so we proceed. Of course we disg

#LeetCode# Container With Most Water (todo)

描述: 实现1 -- 求所有可能的值,O(N^2),超时了(因为超时没有跑所有的测试用例,所以不确定还有没有其他问题) 代码: 1 def maxArea(self, height): 2 tmp = len(height) 3 if tmp == 0 or tmp == 1: return 0 4 if tmp == 2: return abs(height[1] - height[0]) 5 6 minus_lst = [height[i] - height[i-1] for i in ra

[LeetCode][JavaScript]Trapping Rain Water

Trapping Rain Water Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map

[leetcode][042] Trapping Rain Water (Java)

题目在这里: https://leetcode.com/problems/trapping-rain-water/ [标签] Array; Stack; Two Pointers 这个题我感觉很难,我自己是完全不会.下面贴的是别人想到的好方法,自己适当做了些简单调整. 我觉得,这个解法非常巧妙的使用了双向的 two pointers.用到的变量有左右两个指针,一个在最左边,一个在最右边,然后两个一起往中间走.在走的时候维护两个变量,left barrier 和 right barrier,即左边

LeetCode[Array]: Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by a

[leetcode]Container With Most Water @ Python

原题地址:https://oj.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) an

LeetCode 41 Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by a