C#LeetCode刷题之#11-盛最多水的容器(Container With Most Water)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3615 访问。
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (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 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.
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)的更多相关文章
- Leetcode题库——11.盛最多水的容器
@author: ZZQ @software: PyCharm @file: maxArea.py @time: 2018/10/11 21:47 说明:给定 n 个非负整数 a1,a2,...,an ...
- #leetcode刷题之路11-盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- [Swift]LeetCode11. 盛最多水的容器 | Container With Most Water
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- Java实现 LeetCode 11 盛最多水的容器
11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...
- 力扣Leetcode 11. 盛最多水的容器
盛最多水的容器 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...
- leetcode刷题11. 盛最多水的容器
做题连接https://leetcode-cn.com/problems/container-with-most-water/submissions/ 本题分为两种方法: 暴力法: int maxAr ...
- 【LeetCode】11. 盛最多水的容器
题目 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两 ...
- LeetCode 11. 盛最多水的容器(Container With Most Water)
题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两 ...
- Leetcode 11.盛最多水的容器 By Python
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- LeetCode 11 - 盛最多水的容器 - [双指针暴力]
题目链接:https://leetcode-cn.com/problems/container-with-most-water/description/ 给定 n 个非负整数 $a_1,a_2,\cd ...
随机推荐
- Python 爬取异步加载的数据
在我们的工作中,可能会遇到这样的情况:我们需要爬取的数据是通过ajax异步加载的,这样的话通过requests得到的只是一个静态页面,而我们需要的是ajax动态加载的数据! 那我们应该怎么办呢??? ...
- Ethical Hacking - NETWORK PENETRATION TESTING(18)
Session Hijacking What if the user uses the "remember me" feature? If the user uses this f ...
- 年薪50W京东软件测试工程师的成长路——我们都曾一样迷茫
这两天和朋友谈到软件测试的发展,其实软件测试已经在不知不觉中发生了非常大的改变,前几年的软件测试行业还是一个风口,随着不断地转行人员以及毕业的大学生疯狂地涌入软件测试行业,目前软件测试行业“缺口”已经 ...
- nodejs--抓取页面的数据--图
感觉挺有意思,比php好玩 ----做个图留个 纪念
- 网络通信机制:Socket、TCP/IP、HTTP
13.1.1 TCP/IP协议 讲的很抽象,没具体看懂什么是TCP协议,什么是IP协议.IP协议保证消息从一个主机传送到另一个主机,消息在传送的过程中被分割成一个个小包,TCP协议会让两台相互连接的计 ...
- NFS /etc/exports参数解释
nfs 安装 执行以下命令安装 nfs 服务器所需的软件包 yum install -y nfs-utils 执行命令 vim /etc/exports,创建 exports 文件,文件内容如下: / ...
- PHP sscanf() 函数
实例 Parse a string: <?php高佣联盟 www.cgewang.com$str = "age:30 weight:60kg";sscanf($str,&qu ...
- 【NOIP2016】组合数问题 题解(组合数学+递推)
题目链接 题目大意:给定$n,m,k$,求满足$k|C_i^j$的$C_i^j$的个数.$(0\leq i\leq n,1\leq j\leq \min(i,m))$. --------------- ...
- CSS部分样式知识
css文件 /* 注释内容 */ /* 选择器,其中body就是一个选择器,表示选中个body这个标签 声明块:为选择器设置样式 { 样式名: 样式值; } */ body{ background-c ...
- 网络安全传输系统-sprint1传输子系统
一.产品规划与设计 二.传输子系统 基本框架:(1)不带安全功能的传输系统 (2)安全加密功能 part1:基本传输子程序设计(不带安全加密功能) 客户端 服务器 int main(int argc, ...