[LeetCode][11]Container With Most Water解析 时间复杂度为O(n) -Java实现
来源:互联网 发布:linux终端装中文输入法 编辑:程序博客网 时间:2024/05/01 14:04
Q:
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.
A:
这题目还是有点意思的:大概是说在x轴的垂直方向上(即y轴)有一条一条的线,它们以(i,0)开始到(i,ai)结束,挑两个线组成一个最大面积的容器可以装下更多的水。
这里面有一个短板的问题,对于任意两条线ia,ib我们可以得出一个公式S = Math.abs((ia-ib))*Math.min(height[ia],height[ib]).这道题当然我们可以用遍历的方法解决,最简单的遍历时间复杂度应该是O(n^2)很明显这不符合我们的预期。
首先我们要明确一个性质:如果一个容器能有比他更大的容器,突破点一定在高的那个柱子上。
就好像图中的a1区域,要想找到比a1区域更大的区域,肯定最大值在左侧更高的那个柱子上。并且有且只有这一个最大值。
现在我们来严格的证明一下这个性质:
对于1和8来说他们中间相隔的宽度为7,高度8要低一点,假设高为h8,容器面积为s18,那么如果我们不从1找突破点而从8开始的话,如果新的i(1<i<8)的高hi<h8
它的si8 = hi*(8-i)很明显小于s18,翻过来如果hi>h8的话si8 = h8*(8-i)一样的很明显小于s18,所以如果有一个面积大于s18,它的一端一定在1上。
根据上述性质我们终于可以写出来一个时间复杂度为O(n)的程序了
代码如下
public class ContainerWithMostWater {public static void main(String[] args){int[] a ={3,1,9,1};System.out.println(method(a));}private static int method(int[] a) {// TODO Auto-generated method stubint i = 0;int j = a.length-1;int maxS = 0;int thisS = 0;while(i!=j){thisS = Math.min(a[i],a[j])*(j-i);//高*宽maxS = thisS>maxS?thisS:maxS;if(a[i]>a[j]){//突破点在高的柱子上j--;}else {i++;}}return maxS;}}
这一题关于从左右开弓应该大家都想得到,但是证明可能难了点。
- [LeetCode][11]Container With Most Water解析 时间复杂度为O(n) -Java实现
- java 实现Leetcode Container With Most Water
- [leetcode]11. Container With Most Water(Java实现)
- 【leetcode】Container with most water--python实现
- [Leetcode] Container With Most Water (Java)
- 【Leetcode】Container With Most Water in JAVA
- LeetCode: Container With Most Water [Java]
- [LeetCode][Java] Container With Most Water
- LeetCode : Container With Most Water [java]
- leetcode:Container With Most Water 【Java】
- Container With Most Water Leetcode Python Java
- [LeetCode-Java]11. Container With Most Water
- leetcode Container With Most Water(Java)
- Container With Most Water leetcode java
- LeetCode-11-Container With Most Water(C语言实现)
- LeetCode 11 Container With Most Water (C,C++,Java,Python)
- 【小熊刷题】Container with Most Water <Leetcode 11, Java>
- LEETCODE 11 Container With Most Water (JAVA题解)
- MYSQL向日期添加指定的时间间隔
- window.open代替window.showModalDialog(非标准的BOM方法)
- IDM退出机制研究
- Java创建线程的两个方法
- 【Oh-Hard】瘦人增肌是不是要先吃胖?怎么练怎么吃?
- [LeetCode][11]Container With Most Water解析 时间复杂度为O(n) -Java实现
- iMindMap是怎样在学术研究上做贡献的
- CentOS 上通过 yum 快速安装最新版 Transmission
- PCA求解思路
- eclipse, Log4j配置(真心的详细~)
- jmp指令的机器码编写
- Linux下I2C驱动架构全面分析
- 网站建设的基础入门
- 它会知道什么编译的