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. 什么时候用Vector, 什么时候改用ArrayList?

    转自:http://www.cnblogs.com/langtianya/archive/2012/08/28/2659787.html 书得到的信息好像是Vector是从java1开始就有了,Arr ...

  2. 【CodeForces 606A】A -特别水的题1-Magic Spheres

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102271#problem/A Description Carl is a beginne ...

  3. BZOJ2121 字符串游戏

    Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其 他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,BX就可以选择是否将其删 ...

  4. CodeMap

    CodeMap 这是在博客园看到的一位朋友文章介绍的,很好用的插件,所有的方法,注释块在右边一目了然,找代码方便极了,还能设置代码段的高亮,给代码段设置标识

  5. (原)String类两种实例化的区别

    String有两种实例化方式,一种是通过直接赋值的方式,另外一种是使用标准的new调用构造方法完成实例化. public class StringDemo { public static void m ...

  6. 关于Asp.Net Mvc3.0 使用KindEditor4.0 上传图片与文件

    http://blog.csdn.net/fyxq14hao/article/details/7245502 今天我们的Asp.Net Mvc 3的项目中,把KindEditor3.9改为 KindE ...

  7. mysql 时间函数

    select UNIX_TIMESTAMP(Now());#获取unix时间戳1436430994 ::"时间形式

  8. centos 7 的几点改动

    1.运行级别 旧:/etc/inittab 新:/etc/ststemd/system  例:ln -sf /lib/systemd/system/multi-user.target /etc/sys ...

  9. Windows 下配置使用MemCached(转载)

    工具: memcached-1.2.6-win32-bin.zip     MemCached服务端程序(for win) Memcached Manager             win下的Mem ...

  10. ASP.NET MVC 4 跨域

    <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...