【JAVA、C++】LeetCode 011 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.
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的更多相关文章
- 【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 ...
- 【JAVA、C++】LeetCode 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【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 ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- fedora安装软件
jdk 1.下载rpm包 注意32位还是64位,注意是rpm格式 2.安装 sudo rpm -ivh jdk.rpm sudo update-alternatives --config java # ...
- firefox与chrome中对select下拉框中的option支持问题
firefox可以直接修改option的字体样式,但是chrome中option的字体样式是继承select的,这个是在项目中遇到的,具体的可以看一下 http://www.cnblogs.com/r ...
- UOJ150 运输计划
运输计划(transport.cpp/c/pas)[问题描述]公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n-1 条 双向 航道,每条航道建立在两个星球之间,这 n-1 条航道 ...
- 该如何理解AMD ,CMD,CommonJS规范--javascript模块化加载学习总结
是一篇关于javascript模块化AMD,CMD,CommonJS的学习总结,作为记录也给同样对三种方式有疑问的童鞋们,有不对或者偏差之处,望各位大神指出,不胜感激. 本篇默认读者大概知道requi ...
- bzoj2054 疯狂的馒头
bzoj上现在找不到这题,所以目前只是过了样例,没有测 2054: 疯狂的馒头 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 715 Solved: ...
- jQuery键盘事件绑定Enter键
<script> $(function(){ $(document).keydown(function(event){ if(event.keyCode==13){ $("#mo ...
- easyui datagrid使用
http://www.cnblogs.com/zgqys1980/archive/2011/01/04/1925775.html 加载相关js和css,因为easyui依赖jquery,所有加载eas ...
- sqlmap注入检测
1.列出可利用数据库: sqlmap -u url --dbs 2.列出某个数据库中表: sqlmap -u url --tables -D south sqlmap -u ur ...
- C语言之参数传递
学了四年的计算机,一直让自己比较苦恼的问题是C语言的参数传递问题,之所以说是苦恼,是因为在某年的一个学期,不幸接触到数据结构,光一个链表就把自己弄得死去活来的,而且自已一直就楞以为在操作的过程中,传递 ...
- nginx 服务器重启命令,关闭 (转)
nginx -s reload :修改配置后重新加载生效nginx -s reopen :重新打开日志文件nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否 ...