[leetcode刷题系列]Container With Most Water
来源:互联网 发布:淘宝怎么设置促销价格 编辑:程序博客网 时间:2024/06/05 18:22
不做acm很长时间。脑子确实顿了很多。 这题思考了十多分钟至少。其实还是比较简单。
不是很清楚是否存在O(n)的解法。 但是O(nlogn)的解法还是比较好想的。对所有的点按照高度排序, 高的排前面, 挨的排后面。
然后从前往后遍历。 遍历过程中记录下已出现过的坐标的最大值和最小值。然后利用当前坐标的高度去乘以当前坐标距离最大值和最小值当中的记录最长的值,然后去更新答案。最后就是结果了。可能说的有点啰嗦, 看代码还是比较清楚的。
class Solution {public: int maxArea(vector<int> &height) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<pair<int, int> > pv; for(int i = 0; i < height.size(); ++ i) pv.push_back(make_pair(height[i], i)); sort(pv.begin(), pv.end()); reverse(pv.begin(), pv.end()); int in = pv[0].second; int ax = pv[0].second; int ans = 0; for(int i = 1; i < pv.size(); ++ i){ ans = max(ans, pv[i].first * max(abs(pv[i].second - in),abs(pv[i].second - ax))); if(pv[i].second < in) in = pv[i].second; if(pv[i].second > ax) ax = pv[i].second; } return ans; }};
- [leetcode刷题系列]Container With Most Water
- 【Leetcode长征系列】Container With Most Water
- Leetcode-第十一题Container With Most Water
- LeetCode刷题笔录Container With Most Water
- 【小熊刷题】Container with Most Water <Leetcode 11, Java>
- LeetCode: Container With Most Water
- [LeetCode]Container With Most Water
- LeetCode Container With Most Water
- LeetCode: Container With Most Water
- [Leetcode] Container With Most Water
- Leetcode: Container With Most Water
- [LeetCode] Container With Most Water
- LeetCode Container With Most Water
- [Leetcode] Container With Most Water
- [LeetCode]Container With Most Water
- LeetCode-Container With Most Water
- Container With Most Water leetcode
- LeetCode - Container With Most Water
- ORACLE异常——ORA-01000: maximum open cursors exceeded
- 新手该如何入手ibm大型机
- oracle日期截取
- android手机分辨率整理
- php版导出excel
- [leetcode刷题系列]Container With Most Water
- hive的部署
- 设计模式学习--代理模式(Proxy Pattern)
- 现代操作系统笔记之 进程
- Linux环境下安装php网站(服务器,远程)
- DB2索引类型介绍
- 【Android多屏适配】动态改变Listview item高度
- 自定义View的onMeasure方法理解
- 用switch-case一个OnClickListener触发多个button的事件