leetcode刷题目 总结 记录 备忘11
2015-07-29 22:15
483 查看
leetcode11
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.
从两头开始 ,先求一次面积,两边下标差乘两者之间的最低值,即短板,之后开始遍历,数值小的一方往里走,因为是短板,所以得自己往里走,以期能找到一个让对面的数值成为小值的数,才有可能取得最大的面积,之后继续计算面积,与之前的面积相比,替换小的,完成遍历即可。原谅我的语言表述能力较弱。
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.
从两头开始 ,先求一次面积,两边下标差乘两者之间的最低值,即短板,之后开始遍历,数值小的一方往里走,因为是短板,所以得自己往里走,以期能找到一个让对面的数值成为小值的数,才有可能取得最大的面积,之后继续计算面积,与之前的面积相比,替换小的,完成遍历即可。原谅我的语言表述能力较弱。
class Solution { public: int maxArea(vector<int>& height) { int l = 0, r = height.size() - 1; int max = 0; while (l < r) { int temp = (r-l) * min(height[l], height[r]); if (temp > max) max = temp; if (height[l] <= height[r]) l++; else r--; } return max; } };
相关文章推荐
- 静态代理与动态代理
- java要看的书
- 关系数据库还是NoSQL数据库
- 亚马逊的S3服务水平协议(Service Level Agreement,SLA)
- 公有,保护,私有继承基类
- [转载] C-MEX程序编写
- *Delphi容器类之---TOrderedList、TStack、TQueue、TObjectStack、TObjectQueue
- 黑马程序员----java循环结构总结
- 连载二———C语言篇
- NSMutableAttributedString 属性字符串的使用
- 7.5.2 Point-in-Time Recovery Using Event Positions
- C++内存管理
- 解决执行python 脚本一闪而过问题
- 代码整洁之道笔记-有意义的命名
- 解决执行python 脚本一闪而过问题
- 什么是socket ??
- 给数组对象sort函数传递比较函数参数的一点反思
- Spreadsheet Tracking
- 博客开篇
- Intent(有无返回值得跳转)