11. Container With Most Water
来源:互联网 发布:淘宝上万宝贝怎么上传 编辑:程序博客网 时间:2024/05/16 15:08
题目:Container With Most Water
原题链接:https://leetcode.com/problems/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.
给出一组非负整数来表示一组点,数组下标用来表示横坐标,元素值表示纵坐标,每个点与X轴练成一条线,从中任意去两个点的线段与X轴组成一个梯形容器来容水,求最大的容水量。
注意不能倾斜容器。
容器的容量由梯形短的那条边决定。设梯形在X轴上的长度为宽,Y轴上的短边为高,首先选取最宽的点,即数组的第一个点(i = 0)和最后一个点(j = height.size() - 1 ),计算容量,然后往里面收缩,因为宽变小了,所以之后收缩的时候遇到高比原来的矩形高的时候才有可能出现更大的容量,然后一直收缩到i >= j为止。
代码如下:
class Solution {public: int maxArea(vector<int>& height) { int i = 0, j = height.size() - 1, ret = 0; while(i < j) { int h = min(height[i], height[j]); ret = max(ret, (j - i) * h); while(height[i] <= h && i < j) i++; while(height[j] <= h && i < j) j--; } return ret; }};
0 0
- 11.Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11.Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- JMeter 如何把上一个请求的结果作为下一个请求的参数 —— 使用正则提取器
- JDK、J2EE、J2SE、J2ME的区别
- 内存管理问题-NSUserDefaults
- CUDA优化之Unrolling Loop/ 并行粒度
- logistic-regression
- 11. Container With Most Water
- “九头虫”病毒技术分析报告
- uboot 2016.05编译uboot.bin和spl
- 5.nginx源码分析之数据结构:ngx_string
- SLAM刚刚开始的未来
- 【学习框架】jeeSite 【maven项目】导入后转成web项目 【转载加原创】
- windows下简单的音频采集示例
- 关于Kettle的增量更新
- mysql多维数据仓库指南--第一篇第4章(1)