[leetcode]_Container With Most Water

题目:在二维坐标系下,有很多个挡板,有两个挡板之间能够积蓄的水的最大面积。如下图所示:

思路:我只想到暴力解法,用O(n2)的时间复杂度算出任意两个挡板形成的面积,这必须的过不了。

优化解法:O(n).

用两个指针 i 和 j 指向整个height[]数组的头尾。

if i 指向的高度 < j 指向的高度 , then 用 i 指向的高度 * i , j之间的距离 求的面积,i 指针向后移。

(i指针向后移的原因:因为当前的面积由i
的高度决定,相当于现在获得以i指针为一边挡板的最大面积,因为随着j的向内移动,水平宽度肯定是降低的,然后及时j的高度高于i的高度,也因为矩形面积由短板边<i的高度>决定,因此不会比现在的面积更大。)

同理如果 j 指向的高度 < i 指向的高度, 将j指针向内移动。

代码:


 1    public int maxArea(int[] height) {
2 int i = 0 , j = height.length - 1;
3 int max = 0 ;
4 while(i < j){
5 int tempArea = 0;
6 if(height[i] < height[j]){
7 tempArea = height[i]*(j - i);
8 i++;
9 }else{
10 tempArea = height[j]*(j - i);
11 j--;
12 }
13 if(tempArea > max) max = tempArea;
14 }
15 return max;
16 }

这种精妙的算法,代码很简单,思路很难想到。即使知道了正确解法,还是觉得心有不安。待下次遇见类似问题再深化思考吧。

[leetcode]_Container With Most Water,布布扣,bubuko.com

时间: 2024-11-07 00:44:10

[leetcode]_Container With Most Water的相关文章

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

LeetCode 42 Trapping Rain Water(积水体积)

题目链接: https://leetcode.com/problems/trapping-rain-water/?tab=Description Problem: 根据所给数组的值,按照上图的示意图.求解积水最大体积. 首先对所给数组进行遍历操作,求出最大高度所对应的下标为maxIndex 之后从左向右进行遍历,设置左边高度为leftMax 并初始化为 height[0],从i==1到i==maxIndex进行遍历.不断更新water,以及leftMax 当height[I]小于leftMax时