11. Container With Most Water Medium
来源:互联网 发布:怎样加入淘宝外卖 编辑:程序博客网 时间:2024/06/14 00:00
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.
思路: 1.双层循环逐个算出每个容器的装水量,比较选出最大的一个。复杂度为O(n ^2),超时。
2.先算出最外层的容器装水量,若不是最大的,那么最大的容器必定没它宽,而最大的容器高度一定比最外层的高,所以我们每次只需将短边向内移动,比较出最大的容器,复杂度为O(n)。
注意:短板效应。
class Solution {public: int maxArea(vector<int>& height) { int result = 0; for (int i = 0, j = height.size() - 1; i < j;) { int high = min(height[i], height[j]); if (high * (j - i) > result) result = high * (j - i); if (high == height[i]) { i++; } else { j--; } } return result; }};
0 0
- 11. Container With Most Water Medium
- 11. Container With Most Water Medium
- Leetcode 11. Container With Most Water (Medium) (cpp)
- Leetcode 11. Container With Most Water (Medium) (java)
- 【LeetCode】11.Container With Most Water(Medium)解题报告
- [Leetcode 11, Medium] Container With Most Water
- Medium 11题 Container With Most Water
- 11.Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- CSS(2.1)_Know-it-all
- Android 解析服务器json数据
- hdu 2502 月之数(数学)
- 如何解决学vue 1.0和vue 2.0冲突问题
- UGUI 垂直方向CenterOnChild功能的简单实现
- 11. Container With Most Water Medium
- 测试入门--功能测试
- Hanganalyze and Systemstate
- 位运算 实现加法
- 读书笔记 effective c++ Item 15 在资源管理类中提供对原生(raw)资源的访问
- Java 实例
- Git常用命令收集
- 算法训练 字串统计
- 1028. List Sorting (25)