题目: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. maven 私服中央库使用阿里云库

    1.admin登录 进入remote repositories management 2.  设置地址

  2. [转]C#如何判断操作系统位数是32位还是64位

    方法一: 对于C#来说,调用WMI是一种简单易行的方式.我们可以用Win32_Processor类里面的AddressWidth属性来表示系统的位宽.AddressWidth的值受CPU和操作系统的双 ...

  3. ubuntu内部错误的解决办法

    在ubuntu使用过程中,出现下面错误: 对不起,Ubuntu 16.04出现了内部错误. 这并不是ubuntu16.04特有的问题,好像每一个ubuntu版本都有类似的问题. 解决的办法有2个. 1 ...

  4. vim在vps内的终端内支持molokai

    vps的终端内默认的颜色数好像很低.对molokai的支持一直不好. 后查找后得知:vim终端方式默认为16色,而molokai为256配色方案 我以为这是硬件问题,没有解决办法,一直到有一天,我在配 ...

  5. EF操作VS中

    1.安装mysql server 2.安装MySql的VS插件(版本请下载最新版,百度自己搜索,这个不用多说)mysql-for-visualstudio-1.2.3.msi 3.安装用于.net连接 ...

  6. rip中的连续子网以及不连续子网

    RIPv1 RIPv2 距离矢量2 距离矢量 最大跳计数15 最大跳计数15 有类的 无类的 基于广播的    基于组播224.0.09 不支持VLSM 支持VLSM 无认证 允许MD5认证 不支持不 ...

  7. python3.5读取kafka中的数据

    安装包 pykafka 代码如下: from pykafka import KafkaClient client = KafkaClient(hosts="test43:9092" ...

  8. WP架构设计(一)MVVM回顾

    [MVVM的定义]     MVVM的目的是什么? 简单总结起来一句话:分离UI逻辑和业务逻辑.这一点和被大家熟知的MVP和MVC是一致的.     下面详细来说明下这个问题,下面一段英文来自Msdn ...

  9. [开机启动]Linux开机自启和运行级别

    嵌入式系统中程序自启动方法 在很多嵌入式系统中,由于可用资源较少,常常在系统启动后就直接让应用程序自动启动,以减少用户操作和节省资源.如何让自己的应用程序自动启动呢?    在Linux系统中,配置应 ...

  10. js监听文本框变化事件

    用js有两种写法: 法一: <!DOCTYPE HTMl> <html> <head> <title> new document </title& ...