问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. Redis 分布式锁(一)

    前言 本文力争以最简单的语言,以博主自己对分布式锁的理解,按照自己的语言来描述分布式锁的概念.作用.原理.实现.如有错误,还请各位大佬海涵,恳请指正.分布式锁分两篇来讲解,本篇讲解客户端,下一篇讲解r ...

  2. 软件测试大牛都是这样写测试用例的,你get到了嘛?

    1. 用于语句覆盖的基路径法 基路径法保证设计出的测试用例,使程序的每一个可执行语句至少执行一次,即实现语句覆盖.基路径法是理论与应用脱节的典型,基本上没有应用价值,读者稍作了解即可,不必理解和掌握. ...

  3. map数据按照list排序

    简介 (Introduction): 背景 需要对字段进行排序,但是,一个字段的结果存在一个map中,一个存在list中,需要让map的数组,按照list的顺序进行排序 结构图 list顺序 [ { ...

  4. 2020最新全栈必备 Redis,你还不了解么

    什么是Redis Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构,如字符串, 散列, 列表, 集合, 有序集合与范围查 ...

  5. grpc 之 word2pdf使用

    ​ 做一个word转pdf的服务,采用grpc,使用libreoffice命令. 1.构建libreoffice镜像 FROM python:3.6 ENV TZ=Asia/Shanghai RUN ...

  6. SQL之DDL、DML、DCL、TCL

    SQL SQL(structured query language)是一种领域特定语言(DSL,domain-specific language),用于管理关系型数据库(relational data ...

  7. C语言中对文件的读写的一些浅显理解

    前述:基于上学期完成的数据结构的课程设计,对于老师的提出要求实现的基础上,自己在使用过程中发现每次打开程序都需要重新输入数据,于是便决定,将文件读写功能加入此次课程设计中,以下是我的一些心得和浅显理解 ...

  8. Jenkins部署jmx脚本

      针对jenkins+jmeter做接口自动化,jmeter完成测试脚本录入和撰写,而jenkins负责持续集成和报告收集.那么从零实现jenkins可以从以下角度实现 首先需要安装jenkins ...

  9. Java语言概述_章节练习题及面试

    学于尚硅谷开源课程 宋洪康老师主讲 感恩 尚硅谷官网:http://www.atguigu.com 尚硅谷b站:https://space.bilibili.com/302417610?from=se ...

  10. 线程_ThreadLocal

    import threading # 创建ThreadLocal对象 house = threading.local() def process_paper(): user = house.user ...