LeetCode #11 Container With Most Water
来源:互联网 发布:杭州程序员招聘信息 编辑:程序博客网 时间:2024/04/28 18:08
Description
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 and n is at least 2.
Analysis
题目难度为:Medium
思路比较简单,从相距最远的木板开始计算容积,当距离缩短,只有更高的木板才能是容积更大
Code(c++)
class Solution {public: int maxArea(vector<int>& height) { int water = 0, h; int i = 0, j = height.size() - 1; while (i < j) { if (height[i] < height[j]) { h = height[i]; if (water < (j - i) * h) water = (j - i) * h; while (height[i] <= h) ++i; } else { h = height[j]; if (water < (j - i) * h) water = (j - i) * h; while (height[j] <= h) --j; } } return water; }};
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
- 真正意义上的编程第一天
- Android4.4之init
- CSS清除浮动方法集合
- Android 屏幕适配扫盲、教程
- Serial Chart软件使用说明
- LeetCode #11 Container With Most Water
- org.springframework.beans.factory.BeanCreationException: Error creating bean 
- LinkedList的方法分析
- IntelliJ IDEA使用技巧
- 时频特性分析(Matlab)
- lintcode——链表倒数第n个节点
- 部署图
- cccc L2-020. 功夫传人
- Java基础之加解密(四) 对称加密算法使用