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. c#创建ISS站点

    private void CreateWebSite() { try { string installPath = "C:\\Program Files\\MyWeb"; stri ...

  2. 查询条件Where

    1.字符串 $condition = 'name=\'Lily\' and age>10'; 2.数组 ['type' => 1, 'status' => 1] //生成 (type ...

  3. 【poj1274】 The Perfect Stall

    http://poj.org/problem?id=1274 (题目链接) 题意 懒得写了 Solution 二分图匹配裸题.注意清空数组. 代码 // poj3020 #include<alg ...

  4. [NOIP2010] 普及组

    三国游戏 题目内容不放了 由于电脑总是会拆掉最大的组合,所以玩家最多只能得到数值第二大的组合 那么找出第二大的组合就行了 #include<iostream> #include<cs ...

  5. ethtool使用记录

    网卡出现很诡异的问题,把电脑连到一些交换机上是工作的,连到另外一些就不行...交换机上的link灯还时不时的闪一下,看起来像是在尝试连接. 用dmesg查看,看到下面的信息: [ 1112.92211 ...

  6. OMNET++工具的使用(2)

    http://blog.csdn.net/codingkid/article/details/7085214 首先解决一些概念上的问题: 1. 在omnetpp.org中提到的仿真模型和框架与OMNe ...

  7. memcache的内存回收机制

    memcache不会释放内存,而是重新利用. 在缓存的清除方面,memcache是不释放已分配内存.当已分配的内存所在的记录失效后,这段以往的内存空间,memcache只会重复利用. memcache ...

  8. Java Base64、AES、SHA1、MD5加密算法

    package com.example.decript; import java.io.UnsupportedEncodingException; import java.security.Inval ...

  9. linux的设置ip连接crt,修改主机名,映射,建文件

    1.修改IP(或者vim vi /etc/sysconfig/network-scripts/ifcfg-eth0) 2.连接 crt 3.修改主机名 用vim 编辑 /etc/sysconfig/n ...

  10. CSS3 transforms 3D翻开

    R T L B   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...