【leetcode】Container With Most Water(middle)
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.
思路:
肯定是要确定水槽的左右边界位置,需要两个变量l1,l2 分别从最左边和最右边收缩。收缩规则是,比较矮的那一边向中间靠拢。更新水量。
我自己的代码:
int maxArea(vector<int> &height) {
int ans = (height.size() - ) * min(height[], height.back()) ;
int l1, l2, h1, h2;
h1 = ; h2 = height.size() - ;
l1 = h1; l2 = h2;
while(l1 < l2)
{
if(height[l1] > height[h1])
{
h1 = l1;
int tmp = (l2 - l1) * min(height[l2], height[l1]);
ans = (tmp > ans) ? tmp : ans;
}
if(height[l2] > height[h2])
{
h2 = l2;
int tmp = (l2 - l1) * min(height[l2], height[l1]);
ans = (tmp > ans) ? tmp : ans;
}
if(height[l1] < height[l2])
{
l1++;
}
else if(height[l1] > height[l2])
{
l2--;
}
else
{
l1++; l2--;
}
}
return ans;
}
大神的代码就简洁很多:
int maxArea(vector<int> &height) {
int left = , right = height.size() - ;
int maxWater = ;
while(left < right){
maxWater = max(maxWater, (right - left) * min(height[left], height[right]));
height[left] < height[right] ? left++ : right--;
}
return maxWater;
}
【leetcode】Container With Most Water(middle)的更多相关文章
- 【leetcode】Search for a Range(middle)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- 【leetcode】Swap Nodes in Pairs (middle)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【leetcode】Linked List Cycle II (middle)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【leetcode】Reverse Linked List II (middle)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- 【leetcode】Path Sum I & II(middle)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 【leetcode】Binary Search Tree Iterator(middle)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 【leetcode】Validate Binary Search Tree(middle)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【leetcode】Minimum Size Subarray Sum(middle)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【leetcode】Evaluate Reverse Polish Notation(middle)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- Linux命令(6):mv命令
1.作用: 为文件或目录改名或将文件由一个目录移入另一个目录中 2.格式: mv [选项] 源文件或目录 目标文件或目录 3.常见参数: 4.使用实例: [root@localhost ~]# mv ...
- 配置tomcat免安装版服务器
一.首先,确保服务器已经安装java环境,没有tomcat的可以到这里下载 http://tomcat.apache.org/ 二.解压下载的压缩包,我是解压到D盘根目录下的.记住这个目录,后面会用到 ...
- 【转】预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)
用VC++ 2008 编写C语言程序,编译出现错误: 预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反) 解决方法: 建工程时 建立空项目 或者在项目设置里关闭预编 ...
- datatable dateset 载体传递数据、存储过程
第一部分:数据库通过存储过程读取数据,通过datatable接受,前台通过asp:repeater.DataSource()和binding()绑定数据 /// <summary> /// ...
- Win7中修改Chrome浏览器缓存文件目录
方法有两种: 第一种: 在Windows 7下可以用mklink命令把Chrome浏览器的缓存位置设置为自己需要的文件夹路径. Chrome浏览器默认的缓存文件位于: CC:\Users\登录用户名\ ...
- JavaScript学习笔记-数组(1)
数组是值的有序集合.每个值叫做一个元素,而每个元素在数组中有一个位置,以数字表示,称为索引.JavaScript数组是无类型的:数组元素可以是任意类型,并且同一个数组中的不同元素也可能有不同的类型.数 ...
- vs2005_the breakpoint will not currently be hit. The source code is different from the original verison.
公司开发只能用英文版的VS2005 (雅蠛蝶) Tool-->Options-->Debugging->General: 把Require source files to exact ...
- WiFi相关知识
1. 无线路由器的覆盖范围和带机量 覆盖范围取决于发射功率.接收灵敏度,以及对端的发射功率和接收灵敏度.因为Wi-Fi设备在发出数据包后,一定要等待对方回ACK才会确认发送成功,所以,如果任意一方的发 ...
- [转]Nuget挂了的解决方法
今天用Nuget下一个程序包时,发现Nuget挂了:未能解析此远程名称:'nuget.org'.第一反应就是方校长抖威风了,挂个代理上 http://nuget.org 试了下,果然好好的. 用命令n ...
- manifest save for self
一.使用html5的缓存机制 1.先上规则代码:m.manifest CACHE MANIFEST # 2015-04-24 14:20 #直接缓存的文件 CACHE: /templates/spec ...