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 ( ...
随机推荐
- 概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM 各公共网关接口介绍
CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一 ...
- Atitit.软件GUI按钮与仪表盘(01)--报警系统--
Atitit.软件GUI按钮与仪表盘(01)--报警系统-- 1. 温度报警区(鲁大师,360taskman) 1 2. os-区-----cpu_mem_io资源占用监测 1 3. Vm区 1 4. ...
- 【剑指Offer学习】【面试题22:栈的压入、弹出序列】
题目:输入两个整数序列,第一个序列表示栈的压入顺序,请推断二个序列是否为该栈的弹出顺序.假设压入栈的全部数字均不相等. 解题思路: 解决问题非常直观的想法就是建立一个辅助栈.把输入的第一个序列中的数字 ...
- 关于搭配junit 和JUnit报initializationError的解决方法
关于junit是什么就不复述了,网上有 junit的source code是可以下载的,各个版本都有 地址:https://github.com/junit-team 通过junit 的source ...
- linux 下面压缩,解压.rar文件以及rar,unrar实例
http://www.rarlab.com/download.htm [root@bass src]# wget http://www.rarlab.com/rar/rarlinux-x64-5.4. ...
- JVM基础学习之基本概念、可见性与同步
开发高性能并发应用不是一件容易的事情.这类应用的例子包括高性能Web服务器.游戏服务器和搜索引擎爬虫等.这样的应用可能需要同时处理成千上万个请求.对于这样的应用,一般采用多线程或事件驱动的 架构 .对 ...
- web中文字体Font-family应该写什么?
最佳实践是: font-family: Helvetica, Tahoma, Arial, "Microsoft YaHei", "微软雅黑",STXihei, ...
- Unicode Table
Unicode Table http://www.tamasoft.co.jp/en/general-info/unicode.html 00 01 02 03 04 05 06 07 08 09 ...
- ansible之并行运行
ansible之并发运行 转载 2016年07月14日 12:33:39 标签: 并发 / ansible 4474 ansible默认只会创建5个进程,所以一次任务只能同时控制5台机器执行.那如果你 ...
- MongoDB助力快速搭建物流订单系统
简介 快递物流系统里最常见的一种业务类型就是订单的查询和记录.订单的特点是随着递送过程,订单数据需要随时更新路径.数据结构上需要可以灵活应对,这点非常符合Document模型,并且MongoDB支持G ...