leetcode-algorithms-11 Container With Most Water

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.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

解法1

列出所有情况,找到最大值.

class Solution
{
public:
int maxArea(vector<int>& height)
{
int area = 0;
for (int i = 0; i < height.size(); ++i)
{
for (int j = i + 1; j < height.size(); ++j)
{
int low = height[i] > height[j] ? height[j] : height[i];
int temp_area = low * (j - i);
if (temp_area > area)
area = temp_area;
}
} return area;
}
};

时间复杂度: O(n^2).

空间复杂度: O(1).

解法2

要算出最大的面积,不考虑高度的情况下,肯定是长度越长,面积越大.接着,长度减小,面积是由短的那个决定的,因此,要使得到面积更大,就只需要移动短的那个高度.所以可得到下面的算法.

class Solution {
public:
int maxArea(vector<int>& height) {
int area = 0;
int left = 0;
int right = height.size() - 1; while(left < right)
{
int low = height[left] < height[right] ? height[left] : height[right];
int temp_area = low * (right - left);
if (temp_area > area)
area = temp_area; if (height[left] < height[right])
++left;
else
--right;
} return area;
}
};

时间复杂度: O(n).

空间复杂度: O(1).

链接: leetcode-algorithms 目录

leetcode-algorithms-11 Container With Most Water的更多相关文章

  1. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

  3. 【LeetCode】11. Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  4. leetcode problem 11 Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  5. LeetCode OJ 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  6. Leetcode Array 11 Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  7. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  8. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  9. LeetCode Array Medium 11. Container With Most Water

    Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...

  10. leetcode面试准备:Container With Most Water

    leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...

随机推荐

  1. Shiro学习笔记 三(认证授权)

    第一种首先基于角色的权限控制 1.由于不断的创建SecurityFactory工程等步骤重复多次,所以应该将这些步骤封装成一个工具类 还是首先看一下目录结构 主要用到文件 首先贴一下工具类的方法 pa ...

  2. Python GIL(Global Interpreter Lock)

    一.介绍 In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threa ...

  3. gitlab git

    git网站是进不去的需要加权限才能进去!!!!!!!! 登录进去后 ssh-keygen -t rsa -C "gitlab用户名一般是邮箱" 一路设置好 Use the code ...

  4. 使用R语言的RTCGA包获取TCGA数据--转载

    转载生信技能树 https://mp.weixin.qq.com/s/JB_329LCWqo5dY6MLawfEA TCGA数据源 - R包RTCGA的简单介绍 - 首先安装及加载包 - 指定任意基因 ...

  5. 折腾了好久的地图缩放 ngui 各种偷懒实现

    当时找到一篇cocos2dx 地图缩放的  很遗憾我用不了  也要记录一下 免得以后用ugui可以用 转 http://blog.csdn.net/cocosnode/article/details/ ...

  6. JaveWeb 公司项目(7)----- 通过JS动态生成DIV

    Web网页项目的数据表格功能已经大体完成,下面是另一个主要功能,在线视频的显示 目前我做的项目是渔政监控方面,在之前C#的版本中已经实现了摄像头的在线监控,用的海康封装好的SDK,目前需要做的工作是在 ...

  7. Docker个人理解总结

    最新在学习Docker,记录下自己对Docker的理解. 一.Docker是什么? 1. Docker是一个能够把开发的应用程序自动部署到容器的开源引擎. 2.Docker使用Google公司推出的G ...

  8. c语言关键字的区分

    const int a;//声明一个常整型数 int const a;//声明一个常整型数 const int *a;//声明指向常整型数的指针,整型数不可修改,指针可以修改 int * const ...

  9. MySQL学习(七)

    学习子查询 1 查出本网站最新的good_id最大的一条商品(要求取出商品名) mysql> select goos_id,goods_name from goods -> order b ...

  10. CentOS/redhat使用光盘镜像源

    1,首先进行光盘的挂载,注意光盘挂载时不会自动建立目录的,    所以需要自己建立目录.    mkdir /mnt/cdrom    mount /dev/cdrom /mnt/cdrom  #de ...