LeetCode 题解(72): Container With Most Water
来源:互联网 发布:工控 java 编辑:程序博客网 时间:2024/05/29 04:47
题目:
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.
题解:
第一种直观的解法,将高度和原始位置记录在一个struct Node里,对每一个Node按照其height升序排序。然后从排序后的最后一个元素,即height最大的元素开始,计算mostWater,当计算当前元素时,只需要看最左边和最右边的比它高的元素,取其中远的那一个元素。用start和end来记录已经计算过的元素中的当前最左位置和当前最右位置,每次计算前先更新start和end。因为需要排序,所以时间复杂度为O(nlgn)。
struct Node { int index; int height; Node(int i, int h):index(i),height(h){} Node(){} bool operator < (const Node &t) const { return height < t.height; }};class Solution {public: int maxArea(vector<int> &height) { if(height.size() <= 1) return 0; int mostWater = 0; vector<Node> node(height.size()); for(int i = 0; i < height.size(); i++) { node[i].index = i; node[i].height = height[i]; } sort(node.begin(), node.end()); int start = node[height.size()-1].index, end = start; for(int j = height.size() - 2; j >= 0; j--) { start = min(start, node[j].index); end = max(end, node[j].index); mostWater = max(mostWater, max(node[j].height * (node[j].index - start), node[j].height * (end - node[j].index))); } return mostWater; }};
第二种思路:两个指针,头和尾,计算当前mostWater,若头高,减尾指针,若尾高,加头指针。原因是只有这样才有可能增加储水量。时间复杂度O(n)。
C++;
class Solution {public: int maxArea(vector<int> &height) { int length = height.size(); if(length <= 1) return 0; int start = 0, end = height.size()-1, mostWater = 0; if(height[start] <= height[end]) { mostWater = height[start] * (end - start); start++; } else { mostWater = height[end] * (end - start); end--; } while(start < end) { if(height[start] <= height[end]) { mostWater = max(mostWater, height[start] * (end - start)); start++; } else { mostWater = max(mostWater, height[end] * (end - start)); end--; } } return mostWater; }};
java:
public class Solution { public int maxArea(int[] height) { if(height.length <= 1) return 0; int mostWater = 0, start = 0, end = height.length - 1; while(start < end) { mostWater = Math.max(mostWater, Math.min(height[start], height[end]) * (end-start)); if(height[start] <= height[end]) start++; else end--; } return mostWater; }}
Python:
class Solution: # @return an integer def maxArea(self, height): if len(height) <= 1: return 0 mostWater = 0 start = 0 end = len(height) - 1 while start < end: mostWater = max(mostWater, min(height[start], height[end]) * (end - start)) if height[start] <= height[end]: start += 1 else: end -= 1 return mostWater
0 0
- LeetCode 题解(72): Container With Most Water
- 【LeetCode】【Python题解】Container with most water
- leetcode题解||Container With Most Water问题
- LeetCode题解:Container With Most Water
- Leetcode题解:11. Container With Most Water
- LeetCode题解-11-Container With Most Water
- leetcode题解-11. Container With Most Water
- 11. Container With Most Water LeetCode题解
- Leetcode题解-11. Container With Most Water
- LEETCODE 11 Container With Most Water (JAVA题解)
- [LeetCode]Container With Most Water(贪心)
- LeetCode(11) Container With Most Water
- (Leetcode)11. Container With Most Water
- LeetCode (11)Container With Most Water
- LeetCode题解——Container With Most Water
- LeetCode题解(Week 1):Container With Most Water
- leetcode题解Java | 11. Container With Most Water
- [模拟]LeetCode 11. Container With Most Water 题解
- Java工程导入(感叹号)错误提示
- Xcode的快捷键及代码格式化
- OverlayIcon 制作流程及注意项
- uva 1442 Cav(左右各扫描一次)
- odoo8使用onchange改变one2many类型的值
- LeetCode 题解(72): Container With Most Water
- 如何使用Eclipse编写运行Java程序
- VS下控制台运行保持(不要一闪而过)
- 马云:为什么当新的商机来临时,大多数人会“死”在别人的嘴里
- 阿里巴巴微商城加盟---微信号:aqiu558
- 马云入股德邦基金 激活了百大集团?
- 王健林马云齐啖保险"香饽饽"
- 国务院副总理汪洋参观阿里巴巴、临安淘宝村
- LeetCode - Subsets I && II