leetcode11 Container With Most Water
2015-09-27 19:40
411 查看
题意:题的大概意思就是给你一个数组,数组中数字是无序的,每一个数代表一条垂直x轴的线,数值代表长度,问哪两条线和x轴中间的面积最大,题中是说最多能放多少水.
我这翻来覆去一想,这n^2遍历一遍就都知道了,但这个复杂度有点不对啊,至少也应该是一个n*logn或者n的时间复杂度啊,最后看了一下discuss果不其然啊,真的是有n的解法.
其实就是利用贪心的原理,往左缩一位或者往右缩一位,下面贴出源码,就几行代码.
我这翻来覆去一想,这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; }
相关文章推荐
- Kafka错误1_kafka中 FailedToSendMessageException解决_修改host.name
- A child container failed during start
- Contains Duplicate III
- vs2010编译没有问题,然而运行出错Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
- MainPage.xaml
- 使用SDL2出现 “error LNK2019: 无法解析的外部符号 _SDL_main,该符号在函数 _main 中被引用” 时的可能错误记录
- Hdu1163 Eddy's digitai Roots(九余数定理)
- 真机测试INSTALL_FAILED_INSUFFICIENT_STORAGE解决办法
- 如何重装air
- main函数的参数:处理命令行选项
- 2015 ACM/ICPC Asia Regional Shanghai Online 1008
- Handling Complexity in the Halo 2 AI
- Contains Duplicate III
- Training ------- Getting Started
- mybaits标签
- SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
- HDU 5476 Explore Track of Point(几何)——2015 ACM/ICPC Asia Regional Shanghai Online
- 强大的仿苹果启动器 Cairodock
- HDU 5478 Can you find it(快速幂)——2015 ACM/ICPC Asia Regional Shanghai Online
- 2015 ACM/ICPC Asia Regional Shanghai Online