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.

Note: You may not slant the container.

解题思路:

本题是经典的“水箱”问题,最简单的方法是暴力枚举,时间复杂度是O(N^2),当然无法通过。

本题有三个特征:

一、水箱的高度是由最短那根木板决定的

二、最终符合条件的两根木板肯定比他们各自外围的木板长

三、最终符合条件的两个木板的外围高度要小于这两个木板中最短的那个,即最终符合条件的两个木板肯定是所有遍历过的最高的两个(比较拗口,但是是解决本题的关键)

因此我们可以从最外围木板出发,向内遍历,每次替换掉两个之中最短的木板,肯定能遍历到最大容器(目标木板)。

JAVA实现如下:

	static public int maxArea(int[] height) {
if (height.length < 2)
return 0;
int maxV = 0, start = 0, end = height.length - 1;
while (start < end) {
int area = Math.min(height[start], height[end]) * (end - start);
maxV = Math.max(maxV, area);
if (height[start] > height[end])
end--;
else
start++;
}
return maxV;
}

C++:

 #include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
int maxArea(vector<int>& height) {
if (height.size() < )
return ;
int maxV = , start = , end = height.size() - ;
while (start < end) {
maxV = max(maxV, min(height[start], height[end]) * (end - start));
if (height[start] > height[end])
end--;
else
start++;
}
return maxV;
}
};

【JAVA、C++】LeetCode 011 Container With Most Water的更多相关文章

  1. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  2. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  5. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  6. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  7. 【JAVA、C++】LeetCode 006 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  8. 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  9. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. 最短路算法floyd

    内容: 对n个点(n<=450),已知他们的边,也就是相邻关系,求任意两个点的最短距离. 代码: for(int k=1; k<=n; k++)//k写在外面 for(int i=1; i ...

  2. BZOJ-3226 校门外的区间 线段数+拆点(类似的思想)

    shabi题....bzoj关键字检查freopen??可怕,,1A的卡了一小时.... 3226: [Sdoi2008]校门外的区间 Time Limit: 10 Sec Memory Limit: ...

  3. 关于clonezilla

    Clonezilla 是一个很好的系统克隆工具,它可以说是吸取了 Norton Ghost 和 Partition Image 的优点.即不仅支持对整个系统进行克隆,而且也可以克隆单个的分区,这种灵活 ...

  4. jquery------.cycle的使用

    代码下载地址:http://malsup.github.io/jquery.cycle.all.js 把里面的代码复制到jquery.cycle.all.js里面 index.jsp <scri ...

  5. PRML Chapter 1. Introduction

    PRML Chapter 1. Introduction 为了防止忘记,要把每章的重要内容都记下来,从第一章开始 2012@3@28 今天又回去稍微翻了一下第一章内容,发现第一次看的时候没有看透,每次 ...

  6. WinsockExpert+NC抓包上传之拿WEBSHELL

    知识补充: nc上传漏洞在原理上同动网上传漏洞一样,都是利用计算机在读取字符串时,遇到'\0'(00)时,认为字符串结束了,从而丢掉后面的字符串,正如unicode编码特性一样,可被人利用,尽管在这里 ...

  7. mySQL 增量备份方案(转)

    1.在 /etc/my.cnf 下面设置开启bin-log 编辑 vim /etc/my.cnf [mysqld] binlog_format       = MIXED                ...

  8. 使用guava带来的方便

    ​    ​guava是在原先google-collection 的基础上发展过来的,是一个比较优秀的外部开源包,最近项目中使用的比较多,列举一些点.刚刚接触就被guava吸引了... ​    ​这 ...

  9. Jquery 扩展获取RUL参数

    //扩展获取url $.extend({ getUrlVars: function () { var vars = [], hash; var hashes = window.location.hre ...

  10. Spring常用的接口和类(一)

    一.ApplicationContextAware接口 当一个类需要获取ApplicationContext实例时,可以让该类实现ApplicationContextAware接口.代码展示如下: p ...