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 container, such that the container contains the most water.
就是说,x轴上在1,2,...,n点上有许多垂直的线段,长度依次是a1, a2, ..., 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的更多相关文章
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...
- LeetCode 笔记系列 18 Maximal Rectangle [学以致用]
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...
- 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 ...
- LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- 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. ...
- leetcode第11题--Container With Most Water
Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...
- 【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). ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
随机推荐
- mysql string types ---- mysql 字符类型详解
一.mysql 中包涵的字符类型: [national] char [(m)] [character set charset_name] [collate collation_name] [natio ...
- 基于Websocket+SpringMVC4推送部标Jt808终端报警(转)
原文地址:http://www.jt808.com/?p=1263 在开发部标监控平台的时候,我们要及时的将部标终端报警推送到web界面上,以弹窗的形式提供给用户显示,要将报警显示在界面上,部标808 ...
- Visual Studio:error MSB8020
状况如下: error MSB8020: The builds tools for v120 (Platform Toolset = 'v120') cannot be found. To build ...
- Memcached安装以及PHP的调用
Memcached安装以及PHP的调用 [南京·10月17日]OSC源创会开始报名:Swift.大型移动项目构架分享 » 一:安装libevent 由于memcached安装时,需要使用libeven ...
- 找电影资源最强攻略,知道这些你就牛B了!
找电影资源最强攻略,知道这些你就牛B了! 电影工厂 2015-07-01 · 分享 点击题目下方环球电影,关注中国顶尖电影微杂志 我们也许没有机会去走遍千山万水,却可以通过电影进入各种各样的角色来 ...
- java线程阻塞问题排查方法
我开发的worker,每隔几个月线上都会阻塞一次,一直都没查出问题.今天终于了了这个心结.把解决过程总结下和大家分享. 首先用jstack命令打出这个进程的全部线程堆栈.拿到线程dump文件之后,搜索 ...
- C++const关键字用法
const关键字是C++新引进的关键字,目标是用于定义常量,避免C语言中使用宏定义出现的边际问题,并且const是类型安全的,即const定义的是不可修改值的变量,它是有类型的,但是宏替换只是简单的进 ...
- 中兴ZXV10 B860AV1.1 全TTL操作完美破解
本文转自:http://www.znds.com/tv-496624-1-1.html 1)前期准备工作 1.1 拆开盒子,TTL接线,这个论坛里有好多其它帖子,就不再详细描述. 1.2 复制需要安装 ...
- awk 计算数据的和和平均值
awk 计算数据的和和平均值 2014年12月02日 21:11:12 HaveFunInLinux 阅读数:14487更多 个人分类: 小技巧 本文译至:http://d.hatena.ne.j ...
- 精心收集的Hadoop学习资料(持续更新)
转自:http://blog.csdn.net/wypblog/article/details/17528851 最近发现自己收集到的Hadoop学习资料有很多本,想想放在那里也浪费,所以觉得贴出来给 ...