题目: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.

就是说,x轴上在1,2,...,n点上有许多垂直的线段,长度依次是a1a2, ..., an。找出两条线段,使他们和x抽围成的面积最大。面积公式是 Min(ai, aj) X |j - i|

解法1:大家都能想到的,穷举所有(i,j)可能,找一个最大的。

 //O(n^2)
public static int maxArea(int[] height) {
// Start typing your Java solution below
// DO NOT write main() function
int maxArea = 0;
for(int i = 1; i < height.length; i++){
if(height[i] == 0)continue;
for(int j = 0; j < i; j++) {
int area = area(height,i,j);
if(area > maxArea) {
maxArea = area;
}
}
}
return maxArea;
}

O(n^2)穷举

不过这样的话无法通过leetcode大集合。

解法2:可以对解法1中的第二个循环中的j做预先判断从而跳过某些情况。在我们检查比i小的各个j时,计算面积的短板不会超过ai本身。平均到距离上,j不会在一定距离上靠近i。

 public static int maxArea(int[] height) {
// Start typing your Java solution below
// DO NOT write main() function
int maxArea = 0;
for(int i = 1; i < height.length; i++){
if(height[i] == 0)continue;
int maxPossibleIdx = i - maxArea/height[i];
for(int j = 0; j < i && j <= maxPossibleIdx; j++) {
int area = area(height,i,j);
if(area > maxArea) {
maxArea = area;
}
}
}
return maxArea;
}

O(n^2)预判断

这个方法能够通过大集合。

解法3: O(n)的复杂度。保持两个指针i,j;分别指向长度数组的首尾。如果ai 小于aj,则移动i向后(i++)。反之,移动j向前(j--)。如果当前的area大于了所记录的area,替换之。这个想法的基础是,如果i的长度小于j,无论如何移动j,短板在i,不可能找到比当前记录的area更大的值了,只能通过移动i来找到新的可能的更大面积。

 public static int maxArea(int[] height){
int maxArea = 0;
int i = 0;
int j = height.length - 1;
if(j <=0)return 0;
while(i < j) {
int area = area(height, i, j);
if(height[i] < height[j]){
i++; }else {
j--;
}
if(area > maxArea) maxArea = area;
}
return maxArea;
}

O(n)

LeetCode 笔记系列二 Container With Most Water的更多相关文章

  1. LeetCode Array Medium 11. Container With Most Water

    Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...

  2. LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]

    题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...

  3. LeetCode 笔记系列 18 Maximal Rectangle [学以致用]

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...

  4. LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  5. LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  6. LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]

    题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  7. leetcode第11题--Container With Most Water

    Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...

  8. 【LeetCode two_pointer】11. Container With Most Water

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

  9. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

    题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

随机推荐

  1. unity, undo

    如果在操作一个Object之前调用Undo.RecordObject(Object),且操作确实造成Object某些属性的改变,则会产生一个undo记录. 如果我们的架构不是直接操作Object,而是 ...

  2. unity, 用脚本创建mesh

    创建一个空gameObject,添加Mesh Filter和Mesh Renderer两个component,再添加一个脚本createMeshScript: using UnityEngine;us ...

  3. winform程序textbox滚动条保持在最下面 内容不闪烁

    在开发winform程序时,会用到textbox控件来显示信息,当把textbox的Multiline属性改为Ture时(即多行显示状态),ScrollBars属性改为Vertical(内容过多时,显 ...

  4. etcd+calico集群的部署

    etcd单机模式 设置环境变量 1 export HostIP="192.168.12.50" 执行如下命令,打开etcd的客户端连接端口4001和2379.etcd互联端口238 ...

  5. FreeRTOS——1

    以下转载自安富莱电子: http://forum.armfly.com/forum.php FreeRTOS 的特点 FreeRTOS 的主要特点如下:1. 支持抢占式调度,合作式调度和时间片调度.2 ...

  6. 纯CSS炫酷3D旋转立方体进度条特效

    在网站制作中,提高用户体验度是一项非常重要的任务.一个创意设计不但能吸引用户的眼球,还能大大的提高用户的体验.在这篇文章中,我们将大胆的将前面所学的3D立方体和进度条结合起来,制作一款纯CSS3的3D ...

  7. Android基础总结(三)SQLite,ListView,对话框

    测试 黑盒测试 测试逻辑业务 白盒测试 测试逻辑方法 根据测试粒度 方法测试:function test 单元测试:unit test 集成测试:integration test 系统测试:syste ...

  8. 服务器操作系统应该选择 Debian/Ubuntu 还是 CentOS?

    早期,我们使用 Debian 作为服务器软件,后来转向了CentOS,主要原因如下: 1.CentOS/RHEL的生命周期是7年,基本上可以覆盖硬件的生命周期,也就意味着一个新硬件安装以后,不用再次安 ...

  9. go web框架推荐

    https://blog.usejournal.com/top-6-web-frameworks-for-go-as-of-2017-23270e059c4b https://www.zhihu.co ...

  10. 性能加速 - 开启opcache

    说明 PHP 5.5+版本以上的,可以使用PHP自带的opcache开启性能加速(默认是关闭的).对于PHP 5.5以下版本的,需要使用APC加速,这里不说明,可以自行上网搜索PHP APC加速的方法 ...