[11]Container With Most Water
来源:互联网 发布:英镑符号 mac系统 编辑:程序博客网 时间:2024/05/18 00:10
【题目描述】
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.
Subscribe to see which companies asked this question
容器能装多少水取决于最短的木板有多长,所以从0和n-1两头开始计算面积,并同时保持现有的木板长度最长,不然则移到下一木板计算盛水面积。
【代码】
class Solution {public: int maxArea(vector<int>& height) { int n=height.size(); int area=0; int ans=INT_MIN; int i=0; int j=n-1; while(i<j){ area=(j-i)*min(height[j],height[i]); ans=max(area,ans); if(height[j]>=height[i]){ i++; } else{ j--; } } return ans; }};
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
- Container With Most Water
- Container with most water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- 495个C语言问题(摘录)
- Android 源码解析: 图片加载库Picasso 1
- 怎样理解阻塞非阻塞与同步异步的区别?
- 第七周 项目3-负数把正数赶出队列
- 初探SocialFramework和UIActivityViewController
- [11]Container With Most Water
- Android 事件分发机制完全解析(续)
- Student Information System
- 黑马程序员———C语言 函数
- 光照模型的计算方式总结(1.Diffuse分量2.Specular分量 Phong model和Blinn-Phong model (Or Blinn))
- 稀疏矩阵的三元组表示的实现及应用(一)
- MVC经典实例
- UITableView重要点整理
- 第9周项目3-稀疏矩阵的三元组表示的实现及应用(1)