leetcode11 Container With Most Water
来源:互联网 发布:中信淘宝信用卡额度 编辑:程序博客网 时间:2024/04/29 20:17
题意:题的大概意思就是给你一个数组,数组中数字是无序的,每一个数代表一条垂直x轴的线,数值代表长度,问哪两条线和x轴中间的面积最大,题中是说最多能放多少水.
我这翻来覆去一想,这n^2遍历一遍就都知道了,但这个复杂度有点不对啊,至少也应该是一个n*logn或者n的时间复杂度啊,最后看了一下discuss果不其然啊,真的是有n的解法.
其实就是利用贪心的原理,往左缩一位或者往右缩一位,下面贴出源码,就几行代码.
#include "stdio.h"#include "iostream"#include "math.h"#include "vector"using namespace std;class Solution {public: int maxArea(vector<int>& height) { int temp; int left = 0,right = height.size()-1; int result = 0; while(left<right) { result = max(result,min(height[left],height[right])*(right-left)); if(height[left]<height[right]) left++; else right--; } return result; }};int main(){vector<int> num;Solution test;int result;num.push_back(2);num.push_back(5);num.push_back(5);num.push_back(2);num.push_back(3);num.push_back(2);result = test.maxArea(num);cout<<result<<endl;return 0;}
0 0
- Leetcode11: Container with most water
- [LeetCode11]Container With Most Water
- LeetCode11:Container With Most Water
- leetcode11 Container With Most Water
- leetcode11 Container With Most Water
- LeetCode11:Container With Most Water
- Leetcode11 Container With Most Water
- leetcode11:Container With Most Water
- LeetCode11: Container With Most Water
- leetcode11. Container With Most Water
- leetcode11. Container With Most Water
- LeetCode11-Container With Most Water
- LeetCode11. Container With Most Water
- LeetCode11. Container With Most Water
- LeetCode11. Container With Most Water
- leetcode11. Container With Most Water
- LeetCode11. Container With Most Water
- LeetCode11——Container With Most Water
- UML——初识
- HDU 5492 Find a path(DP)——2015 ACM/ICPC Asia Regional Hefei Online
- Java编程【2】 -getFlyAnimal() ->accept an animal list,and return an animal list which contains only anim
- 上下拉刷新之第三方库MJRefresh 的具体使用——(用于UITabView的数据刷新)
- 软考教程复习上
- leetcode11 Container With Most Water
- hdu 5486 Difference of Clustering(合肥网赛)
- UVa 12611 - Beautiful Flag
- Java学习目录(初级篇)
- HDU 4333 Revolving Digits(扩展KMP啊)
- hdu-5481 Desiderium
- java inetAddress类,URL类
- LintCode -- k数和
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds