Leetcode 11. Container With Most Water
来源:互联网 发布:全球最大社交软件 编辑:程序博客网 时间:2024/05/16 09:00
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.
题目大意:有n个长度不一定相等的木板,等间隔排列间隔长度为1,求出两个木板是的它们与x轴围成的矩形(竖直长度选择两个木板中最短值)的最大值。
题目分析:先开始设置头尾两个指标,因为围成的面积大小取决于最短的那个,最长的哪个木板往内移动哪怕找到更长的木板都不行,所以往里移动较短的木板,如果较短的木板长度变长,则面积有可能比原来的大。若比原来的大则记录,否则不记录。
代码如下:
class Solution {public: int maxArea(vector<int>& height) { int i=0,j=height.size()-1,maxarea=0; while(i<j){ int minh=min(height[i],height[j]); maxarea=max(minh*(j-i),maxarea); height[i]<=height[j]?i++:j--; } return maxarea; }};
0 0
- LeetCode 11. Container With Most Water
- [LeetCode]11.Container With Most Water
- LeetCode --- 11. Container With Most Water
- LeetCode 11.Container With Most Water
- [Leetcode] 11. Container With Most Water
- [leetcode] 11.Container With Most Water
- 【leetcode】11. Container With Most Water
- [LeetCode]11. Container With Most Water
- 11. Container With Most Water LeetCode
- leetcode 11. Container With Most Water
- leetcode 11.Container With Most Water
- [LeetCode OJ]11. Container With Most Water
- [leetcode] 11. Container With Most Water
- Leetcode 11. Container With Most Water
- leetcode 11.Container With Most Water
- leetcode 11. Container With Most Water
- LeetCode 11. Container With Most Water
- LeetCode *** 11. Container With Most Water
- Mysql主从复制搭建及详解
- 安卓开发中selector的使用方法详解
- go语言的GDB调试入门初级
- A1339. JZPLCM(顾昱洲)|树状数组|hash表|逆元|分解质因数
- APP或移动端当中h5写的页面,其中的边框写成0.5rem的效果的CSS如下
- Leetcode 11. Container With Most Water
- hashmap实现原理浅析
- The project target (Android 5.0.1) was not properly loaded.
- COM中GUID和UUID、CLSID、IID
- Dithering-视觉的奇特现象
- Microsoft caffe
- position定位解读
- ubuntu下安装rabbitmq-c-master
- 基于SSH框架前后端分页