问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3615 访问。

给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (iai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (iai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

说明:你不能倾斜容器,且 n 的值至少为 2。

图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

输入: [1,8,6,2,5,4,8,3,7]

输出: 49


Given n non-negative integers a1a2, ..., an , where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.

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

Output: 49


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3615 访问。

public class Program {

    public static void Main(string[] args) {
var height = new int[] { 1, 8, 6, 2, 5, 4, 8, 3, 7 }; var res = MaxArea(height);
Console.WriteLine(res); height = new int[] { 5, 7, 2, 0, 9, 1, 5, 16, 25, 36 }; res = MaxArea2(height);
Console.WriteLine(res); Console.ReadKey();
} public static int MaxArea(int[] height) {
//暴力法
var max = 0;
for(var i = 0; i < height.Length; i++) {
for(var j = i + 1; j < height.Length; j++) {
max = Math.Max(max, Math.Min(height[i], height[j]) * (j - i));
}
}
return max;
} public static int MaxArea2(int[] height) {
//双指针法
var max = 0;
var left = 0;
var right = height.Length - 1;
while(left < right) {
max = Math.Max(max, Math.Min(height[left], height[right]) * (right - left));
//“木桶”左右板短的那边向中心移动
if(height[left] < height[right]) left++;
else right--;
}
return max;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3615 访问。

49
56

分析:

显而易见,MaxArea 的时间复杂度为:  ,MaxArea2 的时间复杂度为: 

C#LeetCode刷题之#11-盛最多水的容器(Container With Most Water)的更多相关文章

  1. Leetcode题库——11.盛最多水的容器

    @author: ZZQ @software: PyCharm @file: maxArea.py @time: 2018/10/11 21:47 说明:给定 n 个非负整数 a1,a2,...,an ...

  2. #leetcode刷题之路11-盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  3. [Swift]LeetCode11. 盛最多水的容器 | Container With Most Water

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

  4. Java实现 LeetCode 11 盛最多水的容器

    11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...

  5. 力扣Leetcode 11. 盛最多水的容器

    盛最多水的容器 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...

  6. leetcode刷题11. 盛最多水的容器

    做题连接https://leetcode-cn.com/problems/container-with-most-water/submissions/ 本题分为两种方法: 暴力法: int maxAr ...

  7. 【LeetCode】11. 盛最多水的容器

    题目 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两 ...

  8. LeetCode 11. 盛最多水的容器(Container With Most Water)

    题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两 ...

  9. Leetcode 11.盛最多水的容器 By Python

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  10. LeetCode 11 - 盛最多水的容器 - [双指针暴力]

    题目链接:https://leetcode-cn.com/problems/container-with-most-water/description/ 给定 n 个非负整数 $a_1,a_2,\cd ...

随机推荐

  1. P4554 小明的游戏 (洛谷) 双端队列BFS

    最近没有更新博客,全是因为英语,英语太难了QWQ 洛谷春令营的作业我也不会(我是弱鸡),随机跳了2个题,难度不高,还是讲讲吧,学学新算法也好(可以拿来水博客) 第一题就是这个小明的游戏 小明最近喜欢玩 ...

  2. Python 实现邮件发送功能(进阶)

    上篇文章已经介绍了利用Python发送文本消息的用法,也在文末遗留了如何发送图片和附件的问题,本章主要来回答这两个问题.   本章主要包含知识点: 1. 如何将图片放到邮件主体中发送 2. 如何发送附 ...

  3. 信不信?各种红包App最后都会整合游戏!App+游戏的变现模式分析

    最近两个月「晓衡在线猿码微服」商城上出现了一类需求特别的客户: 我们有个 App,日活用户达到 XXX万,计划在 App 中嵌入游戏增加用户留存和利用流量变现,还有... 看你们这里游戏不少,想了解一 ...

  4. vue配置 less 全局变量

    在使用Vue开发的过程中,通常会用到一些样式的全局变量,如果在每个组件中引入就太繁琐了,维护性也不好,因此全局引入是个不错的想法.下面以less为例,记录一下全局引入less变量的步骤: 1.首先安装 ...

  5. 我和ABP vNext 的故事

    Abp VNext是Abp的.NET Core 版本,但它不仅仅只是代码重写了.Abp团队在过去多年社区和商业版本的反馈上做了很多的改进.包括性能.底层的框架设计,它融合了更多优雅的设计实践.不管你是 ...

  6. 牛客练习赛 66C公因子 题解

    原题 原题 思路 考场想复杂了,搞到自闭-- 实际上,因为差值不变,我们可以先差分,求\(\gcd\),便得到答案(考场时想多了,想到了负数.正数各种复杂的处理,但是不需要),最后处理一下即可 代码 ...

  7. Redis中的Scan命令踩坑记

    1 原本以为自己对redis命令还蛮熟悉的,各种数据模型各种基于redis的骚操作.但是最近在使用redis的scan的命令式却踩了一个坑,顿时发觉自己原来对redis的游标理解的很有限.所以记录下这 ...

  8. 设计模式:state模式

    核心: 把状态的判断逻辑转移到表示不同状态的一系列类当中,可以把复杂的判断逻辑简化 例子: class State //状态接口 { public: virtual void show() = 0; ...

  9. Shell基本语法---函数

    函数 函数定义 function 函数名 () { 指令... return n } 函数调用及参数传递 function func() { echo "第零个参数:" $ #脚本 ...

  10. [redis] -- 过期策略篇

    过期处理 定期删除 redis默认是每隔 100ms 就随机抽取一些设置了过期时间的key,检查其是否过期,如果过期就删除 惰性删除 定期删除可能会导致很多过期 key 到了时间并没有被删除掉.所以就 ...