leetCode-11. Container With Most Water-Medium

descrition

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 and n is at least 2.

解析

方法 1:

暴力法,双重循环检查每一对可能候选 min(a[i], a[j])*abs(j-i),保留最大值,即为面积最大的方案。时间复杂度-O(n^2),空间复杂度-O(1)

方法 2:

时间复杂度-O(n),空间复杂度-O(1)。

对于任意一个候选,面积的计算为:min(a[i], a[j])*abs(j-i),i!=j。两个竖直线的间距 abs(j-i) 越大,面积就会越大,同时矩形面积的大小取决于 a[i], a[j] 中较短的边。

这时我们使用两个指针 ileft,iright 分别从左至右,从右至左遍历数组,直到两则重合停止。这时巨星面积为:area=min(a[ileft], a[iright])*(iright-ileft),使用 maxarea 记录当前最大的面积。一方面,初始时 ileft = 0, iright = n,(iright-ileft) 这一项最大,当两个指针不断往中间靠拢时,间距会逐渐减小;另一方面,面积取决于 a[ileft], a[iright] 较短的边,不是一般性假设 a[ileft] < a[iright],此时如果我们将 iright 往左移,面积将取决于较短的边,area 有可能不会增大,如果我们将 ileft 往右移,将有可能增大 area,因为当前 a[ileft] 是限制面积增长的关键。

leetcode-solution 很好的解释,通过动态规划的思想(类似最短路径的求解思想),或反证法可以证明算法的正确性。

code


#include <iostream>
#include <vector>
#include <algorithm> using namespace std; class Solution{
public:
int maxArea(vector<int>& height){
int maximum = 0;
int ileft = 0, iright = height.size() - 1;
while(ileft < iright){
int area = min(height[iright], height[ileft]) * (iright - ileft);
maximum = max(area, maximum);
if(height[ileft] < height[iright])
ileft++;
else
iright--;
} return maximum;
}
}; int main()
{
return 0;
}

[array] leetCode-11. Container With Most Water-Medium的更多相关文章

  1. [leetcode] 11. Container With Most Water (medium)

    原题链接 以Y坐标长度作为木桶边界,以X坐标差为桶底,找出可装多少水. 思路: 前后遍历. Runtime: 5 ms, faster than 95.28% of Java class Soluti ...

  2. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  3. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  4. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  5. 蜗牛慢慢爬 LeetCode 11. Container With Most Water [Difficulty: Medium]

    题目 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...

  6. LeetCode 11. Container With Most Water (装最多水的容器)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  7. [LeetCode] 11. Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  8. [leetcode]11. Container With Most Water存水最多的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  9. [LeetCode]11. Container With Most Water 盛最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  10. LeetCode#11. Container With Most Water

    问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...

随机推荐

  1. Android 关于编译ijkplayer下的so经验分享

    前言:公司最近需要做直播方面的技术调研,所以需要去研究播放器相关的技术:刚好本人github上收藏了ijkplayer,之前一直没有研究过,现在刚好clone下来研究研究. 我先在Windows安装c ...

  2. Java学习笔记十

    网络编程: 一.osi和TCP协议对照: 二.通讯三要素: 三.InetAddress对象简述: import java.net.InetAddress; import java.net.Unknow ...

  3. HDU 1576 A/B 数论水题

    http://acm.hdu.edu.cn/showproblem.php?pid=1576 写了个ex_gcd的模板...太蠢导致推了很久的公式 这里推导一下: 因为 1 = BX + 9973Y ...

  4. 单调栈+贪心维护LIS

    普通:O(\(N^2\)) 状态:dp[j]表示,以j结尾的最长的上升子序列 转移:dp[j]=dp[i]+1(if a[j]>a[i] ) 初始化:dp[i]=1 优化(nlogn) solu ...

  5. postgresql 不同数据库不同模式下的数据迁移

    编写不容易,转载请注明出处谢谢, 数据迁移 因为之前爬虫的时候,一部分数据并没有上传到服务器,在本地.本来用的就是postgresql,也没用多久,数据迁移的时候,也遇到了很多问题,第一次使pg_du ...

  6. 使用TCP协议的NAT穿透技术 (转载)

    其实很早我就已经实现了使用TCP协议穿透NAT了,但是苦于一直没有时间,所以没有写出来,现在终于放假有一点空闲,于是写出来共享之. 一直以来,说起NAT穿透,很多人都会被告知使用UDP打孔这个技术,基 ...

  7. Flume的Collector

    Collector的作用是将多个Agent的数据汇总后,加载到Storage中.它的source和sink与agent类似. 数据源(source),如: collectorSource[(port) ...

  8. DNS Flood Detector让DNS更安全

    650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...

  9. 很好的资源 for android

    //texttospeach http://examples.javacodegeeks.com/android/core/text-to-speech/android-text-to-speech- ...

  10. 推广一下新Blog www.hrwhisper.me

    新博客地址:www.hrwhisper.me 欢迎互访加友链~