题目地址
https://leetcode.com/problems/container-with-most-water/description/
题目描述
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
思路
符合直觉的解法是,我们可以对两两进行求解,计算可以承载的水量。 然后不断更新最大值,最后返回最大值即可。
这种解法,需要两层循环,时间复杂度是O(n^2)
eg:
// 这个解法比较暴力,效率比较低 // 时间复杂度是O(n^2) let max = 0; for(let i = 0; i < height.length; i++) { for(let j = i + 1; j < height.length; j++) { const currentArea = Math.abs(i - j) * Math.min(height[i], height[j]); if (currentArea > max) { max = currentArea; } } } return max;
这种符合直觉的解法有点像冒泡排序, 大家可以稍微类比一下
那么有没有更加优的解法呢?我们来换个角度来思考这个问题,上述的解法是通过两两组合,这无疑是完备的,
那我门是否可以先计算长度为n的面积,然后计算长度为n-1的面积,... 计算长度为1的面积。 这样去不断更新最大值呢?
很显然这种解法也是完备的,但是似乎时间复杂度还是O(n ^ 2), 不要着急。
考虑一下,如果我们计算n-1长度的面积的时候,是直接直接排除一半的结果的。
如图:
比如我们计算n面积的时候,假如左侧的线段高度比右侧的高度低,那么我们通过左移右指针来将长度缩短为n-1的做法是没有意义的,
因为新的形成的面积变成了(n-1) * heightOfLeft 这个面积一定比刚才的长度为n的面积nn * heightOfLeft 小
也就是说最大面积一定是当前的面积或者通过移动短的线段得到
。
关键点解析
- 双指针优化时间复杂度
代码
- 语言支持:JS,C++
JavaScript Code:
/* * @lc app=leetcode id=11 lang=javascript * * [11] Container With Most Water * * https://leetcode.com/problems/container-with-most-water/description/ * * algorithms * Medium (42.86%) * Total Accepted: 344.3K * Total Submissions: 790.1K * Testcase Example: '[1,8,6,2,5,4,8,3,7]' * * 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 * */ /** * @param {number[]} height * @return {number} */ var maxArea = function(height) { if (!height || height.length <= 1) return 0; // 双指针来进行优化 // 时间复杂度是O(n) let leftPos = 0; let rightPos = height.length - 1; let max = 0; while(leftPos < rightPos) { const currentArea = Math.abs(leftPos - rightPos) * Math.min(height[leftPos] , height[rightPos]); if (currentArea > max) { max = currentArea; } // 更新小的 if (height[leftPos] < height[rightPos]) { leftPos++; } else { // 如果相等就随便了 rightPos--; } } return max; };
C++ Code:
class Solution { public: int maxArea(vector<int>& height) { auto ret = 0ul, leftPos = 0ul, rightPos = height.size() - 1; while( leftPos < rightPos) { ret = std::max(ret, std::min(height[leftPos], height[rightPos]) * (rightPos - leftPos)); if (height[leftPos] < height[rightPos]) ++leftPos; else --rightPos; } return ret; } };LeetCode题解堆排序和快速排序
堆排序和快速排序都是时间复杂度$O(nlogn)$ 的算法,其中 n 为数据规模。 那么两者谁更快呢? 为什么?题解:快排最坏情况下是O(n^2),平均和最好是O(nlogn) ,堆排序始终为O(nlogn),还是堆排序快吧
LeetCode题解斜着遍历遍历是算法的基础。 我们平时看到的 DFS 和 BFS 都是搜索, 而搜索的核心就是遍历,而关键点就是遍历的方式。 从根本上说动态规划也是枚举所有的可能,而枚举就需要用到遍历。 而平时遍历一个二维数组 martrix 的时候, 我们习惯的方式是按行从左到右或者从右到左遍历。 少有情况是按照列遍历, 更少有情况是斜着遍历。那么这次就考考你, 怎么斜着遍历一个二…
LeetCode题解砝码的最小数量假设有三种重量的砝码,2g、3g、7g,对于任意重量物品,请设计一个函数getResult(weight),接收一个参数weight,返回所需砝码的最小数量。输入示例:const weight = 100;输出示例:getResult(weight) // 15 其中7g的14个,2g的1个题解:```phpfunction getResult($weigh…
LeetCode题解93. 复原IP地址给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。有效的 IP 地址正好由四个整数(每个整数位于 0 到 255 之间组成),整数之间用 '.' 分隔。 示例:输入: \"25525511135\"输出: [\"255.255.11.135\", \"255.255.111.35\"]来源:力扣(LeetCode)链接:https://le…
可以在没有操作系统的情况下运行Java程序吗? - java我知道所有Java程序都由JVM执行。这使Java与所有操作系统兼容(一次编写,可在任何地方运行)。但是我可以在没有操作系统的情况下运行Java程序吗?也许只运行JVM?并且,如果可能,功能是否会受到任何影响?注意:我的主要问题是,java程序可以直接在硬件上运行(通过JVM)吗?我可以在计算机中“启动”任何低级别的JVM吗? java大神给出的解决方案 实…