LeetCode-11 Container With Most Water
来源:互联网 发布:遂了其事乎 编辑:程序博客网 时间:2024/05/24 04:31
题目:给定一个数组序列a[n],找出两个值,使以者两个值围成的区域可以容纳最多的水
思路:设a[0]和a[n-1]容纳的水为T,则a[m]和a[n]容纳的水大于T只可能在a[m]大于a[0]或者a[n]大于a[n-1]的情况。故可以选择从两端向中间逼近,查找最大值,时间复杂度O(N),
class Solution {public: int maxArea(vector<int> &height){ long long max_contain=0; long long val=0; int i=0;int j=height.size()-1; long long temp; while(i<j){ if(height[i]>=height[j])temp=height[j]*(j-i); else temp=height[i]*(j-i); if(temp>max_contain)max_contain=temp; if(height[i]<height[j]){ int m=i; while(height[m]<=height[i]&&m<j)m++; i=m; } else{ int n=j; while(height[n]<=height[j]&&n>i)n--; j=n; } } if(max_contain>2147483647)return 2147483647; else return max_contain; }};
0 0
- LeetCode 11: Container With Most Water
- leetcode 11 Container With Most Water
- Leetcode【11】:Container With Most Water
- [leetcode 11] Container With Most Water
- [leetcode 11] Container With Most Water
- LeetCode | #11 Container With Most Water
- leetcode.11---------Container With Most Water
- [LeetCode 11]Container With Most Water
- leetcode 11 Container With Most Water
- leetcode-11 Container With Most Water
- Container With Most Water - LeetCode 11
- [Leetcode 11, Medium] Container With Most Water
- LeetCode 11 - Container With Most Water
- leetcode 11 -- Container With Most Water
- #LeetCode OJ #11 Container With Most Water
- Leetcode #11 Container With Most Water
- leetcode-11Container With Most Water
- leetcode 11 -- Container With Most Water
- Android BLE开发之BluetoothGatt status 133
- ProGuard Version5.2(4.1)Manual-->Introduction
- MySQL 序列生成函数
- Realm [realm.ShiroDbRealm@15408475] does not support authentication token
- eclipse 转 android studio (使用及遇到的问题集合)
- LeetCode-11 Container With Most Water
- GCD·我所理解的扩展欧几里得
- ubuntu强制关机造成grub损坏,开机黑屏进入grub自负界面修复
- captive portal
- 尺取法
- 静态代码块,构造代码块和构造函数的区别
- Log4j 2.x使用注意事项
- python连接mongodb并操作
- 设计模式--代理模式