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 的平方根
相关文章:

微软极品Sysinternals Suite工具包使用指南
微软极品Sysinternals Suite工具包使用指南 按照名称首字母排序,点击每个蓝色标题链接都可以转到微软的对应官方页面,有对这些工具包的直接下载地址和更详尽的用法。因为每个软件几乎都可以长篇大论的介绍,所以,在此就只做简介和罗…
【布局】圣杯布局双飞翼布局
背景 随着前端技术的发展推进,web端的布局方式已基本成熟,那么在网站布局方式中,三列布局最为常用,布局方式也有很多,渐渐的开发者们开始从效率的角度优化自己的代码“如果三排布局能将中间的模块放在dom树前面&#x…

UI设计师面试如何操作才能获得高薪
UI设计师在近几年是非常吃香的,求职招聘网站上对于UI设计师的要求也越来越高,那么在面试的过程中UI设计师面试如何操作才能获得高薪呢?来看看下面的详细解析。 UI设计师面试如何操作才能获得高薪? 1、行为举止得体大方 我们先从仪态体态方面说…

HDU2673-shǎ崽(水题)
如果不能够直接秒杀的题,就不算水题。又应证了那句话,有时候,如果在水题上卡住,那么此题对于你来说,也就不算是水题了额~~ 刚睡醒,迷迷糊糊。 题目的意思很简单,求一个最大的,再求一…

center os7 安装mysql
安装mariadb MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。M…

LeetCode实战:最长公共前缀
题目英文 Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string “”. Example 1: Input: ["flower","flow","flight"] Output: "fl"…

软件测试培训需要学习什么技术
软件测试技术相对于IT行业的其他技术,学习起来是比较简单的,大多数零基础学员想要进入到IT行业都会优先选择软件测试,那么具体软件测试培训需要学习什么技术呢?来看看下面的介绍就知道了。 软件测试培训需要学习什么技术? 每个软件在上线之…

检测晃动的三种方法
http://stackoverflow.com/questions/150446/how-do-i-detect-when-someone-shakes-an-iphone 我的实现(基于Eran Talmor): 没必要application.applicationSupportsShakeToEdit YES; Set the applicationSupportsShakeToEdit property in th…

android随手记
Linearlayout: gravity:本元素中所有子元素的重力方向 layout_gravity:本元素对于父元素的重力方向 自定义权限:http://www.cnblogs.com/it-tomorrow/p/4115161.html 注意:1 .在被调用时就算是normal权限也需要在加入,不然会permission Deney,在…

LeetCode实战:最接近的三数之和
题目英文 Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given ar…

零基础学习UI设计有哪些简单有效的方法
UI设计的普及让越来越多的人对UI有了重新的认识,很多企业对UI设计这个岗位都是非常重视的,如今很多零基础学员都想要转行做UI设计,那么针对零基础学习UI设计有哪些简单有效的方法呢?来看看下面的详细介绍吧。 零基础学习UI设计有哪些简单有效…

(转)linux下oracle instant client安装和运行
1.首先要知道什么是ORACLE的客户端: Oracle Instant client 是oracle提供的简便客户端, 支持多种平台. 可从oracle网站下载, 下载地址为http://www.oracle.com/technology/tech/oci/instantclient/index.html 包括如下内容: Instant client Package - Basic 运行OCI…

LeetCode实战:删除排序数组中的重复项
题目英文 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra me…

16G 手机清理
1.16G 手机清理 清理top 5 的应用的缓存即可 2,hw wife 连接模块 低于 app wifi 的连接模块。 在同样的电脑热点面前,hw 连补上电脑热点,apple 可以连上电脑热点。 其他差异不大。 2.奇兔刷机 tencent应用宝 and 手机管理,备份软件 http://w…

java培训有哪些收费标准
随着学习java技术的人越来越多,市面上的java培训机构也越来越多,每家机构的收费标准都不一样,这让很多想要学习java技术的小伙伴都比较头疼,下面小编就为大家详细的介绍一下java培训有哪些收费标准?如何来评判适合自己的java培训…

使用ASP.NET操作IIS7中使用应用程序
在最新发布的启明星Portal里,增加了安装程序,下面说一下.NET对IIS7操作。IIS7的操作和IIS5/6有很大的不同,在IIS7里增加了 Microsoft.Web.Administration 命名空间里,增加了ServerManager、Site几个大类来操作IIS7。 下面是一些核…

MYSQL体系结构-来自期刊
MYSQL体系结构-来自期刊 MySQL三层体系结构|-----------------------------------------------------------------------------------| | mysqld-SQL层 | |-------------------------------------------…

LeetCode实战:搜索旋转排序数组
题目英文 Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). You are given a target value to search. If found in the array return its index, otherwise retur…

Python内置数据结构之双向队列
经常听说Python就是一门执行速度低的语言,可能是你的程序中使用了复杂的算法与数据结构,才会导致程序执行速率低的。在Python的标准库中提供了常见的数据结构工开发者使用,不仅执行速率比较快,还可以简化开发者的编程工作。下面我…

C# GDAL 学习一
最近一直琢磨如何用C#GDAL读取栅格数据(.tif或.img),运气不错的在GDAL 的官网上找到一部分源码。经过本人测试,效果还不错。学习还将继续深入下去。 参考网址:http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/csh…

LeetCode实战:字符串相加
题目英文 Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100.Both num1 and num2 contains only digits 0-9.Both num1 and num2 does not contain any leadin…

js中修改this的指向方法整理
JavaScript(简称“JS”) 是一种具有函数优先的轻量级,解释型或即时编译型的编程语言。虽然它是作为开发Web页面的脚本语言而出名,但是它也被用到了很多非浏览器环境中,JavaScript 基于原型编程、多范式的动态脚本语言,并且支持面向…

VC运行时库(/MD、/MT等)
VC项目属性→配置属性→C/C→代码生成→运行时库 可以采用的方式有:多线程(/MT)、多线程调试(/MTd)、多线程DLL(/MD)、多线程调试DLL(/MDd)、单线程(/ML)、单线程调试(/MLd)。 Reusable LibrarySwitchLibraryMacro(s) DefinedSingle Threaded/MLLIBC(none)Static Mu…

函数式编程概述
2019独角兽企业重金招聘Python工程师标准>>> 引子 JDK8中引入了lambda函数式编程的概念,那么什么是函数式编程,函数式编程又有什么好处,今天我们就来说说函数式编程 我们先了解一下函数式编程的由来 一个名叫阿隆佐邱奇的数学家设…

LeetCode实战:字符串相乘
题目英文 Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Example 1: Input: num1 "2", num2 "3" Output: "6"Example 2: Input: num1 &q…

UI培训教程之系统图标如何设计?
UI设计在近几年广受大家的关注,学习UI设计的人越来也多,今天小编要介绍的就是其中的系统图标设计方法,系统图标在UI设计中是非常基础的图形化语言,也在页面交互中起到很重要的作用。单个图标的设计并不难,但是系统化、…

看懂SQL Server的查询计划(绝对好文!)
在园子看到一篇SQLServer关于查询计划的好文,激动啊,特转载。原文出自:http://www.cnblogs.com/fish-li/archive/2011/06/06/2073626.html看懂SqlServer查询计划对于SqlServer的优化来说,可能优化查询是很常见的事情。关于数据库的优化,本身也…
LeetCode实战:全排列
题目英文 Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] ]题目中文 给定一个没有重复数字的序列,返回其所有可能的全排列。 示例: 输入: …

git pull出现There is no tracking information for the current branch
使用git pull 或者 git push 的时候报错 gitThere is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details git pull <remote> <branch> If you wish to set tracking information…

零基础快速学习Java技术的方法整理
在学习java技术这条道路上,有很多都是零基础学员,他们对于java的学习有着很多的不解,不知怎么学习也不知道如何下手,其实Java编程涉及到的知识点还是非常多的,我们需要制定java学习路线图这样才能少走弯路,…