LeetCode实战:盛最多水的容器
题目英文
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
题目中文
给定 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
算法实现
利用暴力算法
public class Solution {public int MaxArea(int[] height) {int max = int.MinValue;for (int i = 0; i < height.Length - 1; i++){for (int j = 1; j < height.Length; j++){int temp = (j - i)*Math.Min(height[i], height[j]);if (temp > max){max = temp;}}}return max; }
}
利用双指针算法
以0-7走到1-7这一步为例,解释为什么放弃0-6这一分支:
用h(i)表示第i条线段的高度,S(ij)表示第i条线段和第j条线段圈起来的面积。已知 h(0) < h(7),从而S(07) = h(0) * 7。有S(06) = min(h(0), h(6)) * 6。当h(0) <= h(6),有S(06) = h(0) * 6;
当h(0) > h(6),有S(06) = h(6) * 6,S(06) < h(0) * 6。由此可知,S(06)必然小于S(07)。
把每一棵子树按照同样的方法分析,很容易可以知道,双指针法走的路径包含了最大面积。
参考图文:
https://leetcode-cn.com/problems/container-with-most-water/solution/zhi-guan-de-shuang-zhi-zhen-fa-jie-shi-by-na-kong/
public class Solution {public int MaxArea(int[] height) {int i = 0, j = height.Length - 1;int max = int.MinValue;while (i < j){int temp = (j - i) * Math.Min(height[i], height[j]);if (temp > max){max = temp;}if (height[i] < height[j]){i++;}else{j--;}}return max; }
}
实验结果
利用暴力算法
- 状态:超出时间限制
- 49 / 50 个通过测试用例
利用双指针算法
- 状态:通过
- 50 / 50 个通过测试用例
- 执行用时: 144 ms, 在所有 C# 提交中击败了 99.64% 的用户
- 内存消耗: 26.6 MB, 在所有 C# 提交中击败了 5.45% 的用户
相关图文
1. “数组”类算法
- LeetCode实战:三数之和
- LeetCode实战:求众数
- LeetCode实战:缺失的第一个正数
- LeetCode实战:快乐数
- LeetCode实战:寻找两个有序数组的中位数
2. “链表”类算法
- LeetCode实战:两数相加
- LeetCode实战:删除链表的倒数第N个节点
- LeetCode实战:合并两个有序链表
- LeetCode实战:合并K个排序链表
- LeetCode实战:两两交换链表中的节点
- LeetCode实战:旋转链表
- LeetCode实战:环形链表
3. “栈”类算法
- LeetCode实战:有效的括号
- LeetCode实战:最长有效括号
- LeetCode实战:逆波兰表达式求值
4. “队列”类算法
- LeetCode实战:设计循环双端队列
- LeetCode实战:滑动窗口最大值
- LeetCode实战:整数反转
- LeetCode实战:字符串转换整数 (atoi)
5. “递归”类算法
- LeetCode实战:爬楼梯
6. “字符串”类算法
- LeetCode实战:反转字符串
- LeetCode实战:翻转字符串里的单词
7. “树”类算法
- LeetCode实战:相同的树
- LeetCode实战:对称二叉树
- LeetCode实战:二叉树的最大深度
- LeetCode实战:将有序数组转换为二叉搜索树
8. “哈希”类算法
- LeetCode实战:两数之和
9. “搜索”类算法
- LeetCode实战:搜索二维矩阵
10. “动态规划”类算法
- LeetCode实战:最长回文子串
11. “数值分析”类算法
- LeetCode实战:x 的平方根
LeetCode实战:盛最多水的容器相关推荐
- [贪心|双指针] leetcode 11 盛最多水的容器
[贪心|双指针] leetcode 11 盛最多水的容器 1.题目 题目链接 给你 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 ...
- LeetCode.M11.盛最多水的容器
LeetCode.M11 题目: 题目大意: 如图所示. 数据范围: 如图所示 思路: 采用双指针,所容纳的水为min(h[l], h[r]) * (r - l) ,初始时l = 0,r = ...
- LeetCode 11. 盛最多水的容器
11. 盛最多水的容器 思路:双指针,放弃低的那边 class Solution { public:int maxArea(vector<int>& height) {int n= ...
- 20200120:(leetcode)盛最多水的容器 两种解法
盛最多水的容器 题目 基本思路 代码实现 题目 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ...
- 【leetcode】 盛最多水的容器
一.题目描述 给定一个长度为 n 的整数数组 height .有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) . 找出其中的两条线,使得它们与 x 轴共同构成 ...
- LeetCode 11. 盛最多水的容器(双指针)
文章目录 1. 题目信息 2. 解题 1. 题目信息 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) . 在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 ...
- leetcode —— 11. 盛最多水的容器
给定 n 个非负整数 a1,a2,...,ana_1,a_2,...,a_na1,a2,...,an,每个数代表坐标中的一个点 (i,ai)(i, a_i)(i,ai) .在坐标内画 n 条垂 ...
- 6. Leetcode 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/ 解决方案 class Solution {public int maxArea( ...
最新文章
- ubuntu终端基础命令
- markdown设置字体颜色大小、目录、列举和横线
- 使用Fabric自动化你的任务
- 【Spring注解系列12】@Value与@PropertySource注解
- ie bug(如果不足,留言大家一起分享)
- hyperion高光谱参数_收藏!光纤光谱仪在激光领域的典型应用
- Django(part18)--静态文件
- python考试有什么用_Python有什么用?2020年学习Python的10个理由
- 嘉年华ON LINE首次在墨天轮和视频号并机直播,数据库内核技术抢先get
- 前端面试之 判断 true == true 需要进行哪几步操作?
- Flex请求Php端的奇怪现象
- python判断负数_python中的负数
- php如何获取百度快照,php代码获取 百度收录和百度快照时间
- Google 回归中国,你准备好成为 Googler 了吗?
- 30_Python基础_异常
- 数据库 MySQL 中 DQL 数据库查询语言(特别重要)
- Android 详细讲解修改app状态栏颜色
- oracle数据库数据导入导出步骤
- Java中的多线程安全问题
- 静态成员和非静态成员的区别
热门文章
- 1053 Path of Equal Weight
- ZJU-java进阶笔记 第六周(抽象与接口)
- beats x连android手机吗,beats x 能连安卓手机吗?
- oracle创建DBA角色命令,oracle常用DBA命令
- 【逆序对】Ultra - Quicksort
- Database Appliance并非Mini版的Exadata-还原真实的Oracle Unbreakable Database Appliance
- Ubuntu 15.10安装ns2.35+nam
- JDBC实例--工具类升级,使用Apache DBCP连接池重构DBUtility,让连接数据库更有效,更安全...
- IDEA如何导入多个maven依赖的项目
- Hashtable,HashMap,ConcurrentHashMap都是Map的实现类,它们在处理null值的存储上有细微的区别,下列哪些说法是正确的