leetcode---Container With Most Water
2016-05-24 18:30
489 查看
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.
Note: You may not slant the container.
class Solution { public: int maxArea(vector<int>& height) { int ans = 0; int n = height.size(); int l = 0, r = n-1; while(l < r) { ans = max(ans, min(height[l], height[r])*(r-l)); if(height[l] < height[r]) { int k = l; while(k<r && height[k] <= height[l]) k++; l = k; } else { int k = r; while(k>l && height[k] <= height[r]) k--; r = k; } } return ans; } };
相关文章推荐
- 我国发展人工智能由谁牵头?
- linker command failed with exit code 1 (use -v to see invocation)
- 使用Genymotion出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决方法
- 关于gmail注册时此电话号码无法用于进行验证
- 关于Fiddler 出现 Creation of interception certificate failed.提示的解决办法
- 直接插入排序(Straight Insertion Sort)
- 直接选择排序(Straight Selection Sort)
- Contains Duplicate II
- HDOJ/HDU 1039 Easier Done Than Said?(字符串处理~)
- HDOJ/HDU 1039 Easier Done Than Said?(字符串处理~)
- Windows下wamp sever+sendmail使用mail()函数时的配置
- Open Baidu webpage and type in Chinese characters to start searching, press “Back” button to go to B
- file_put_contents 错误:failed to open stream: Invalid argument 一种原因
- Contains Duplicate
- Net::IMAP::Simple, login(), mailboxes(), select(), get()
- Net::SMTP mail(), to(), data(), datasend(), auth()
- 23种设计模式(21)--Chain of Responsibility
- 一步步学spark之一scala面向对象Trait 比较重要2.6
- 结构体的另外一种形式(pair)
- git如何解决failed to push some refs to git