Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.

思路:

肯定是要确定水槽的左右边界位置,需要两个变量l1,l2 分别从最左边和最右边收缩。收缩规则是,比较矮的那一边向中间靠拢。更新水量。

我自己的代码:

int maxArea(vector<int> &height) {
int ans = (height.size() - ) * min(height[], height.back()) ;
int l1, l2, h1, h2;
h1 = ; h2 = height.size() - ;
l1 = h1; l2 = h2;
while(l1 < l2)
{
if(height[l1] > height[h1])
{
h1 = l1;
int tmp = (l2 - l1) * min(height[l2], height[l1]);
ans = (tmp > ans) ? tmp : ans;
}
if(height[l2] > height[h2])
{
h2 = l2;
int tmp = (l2 - l1) * min(height[l2], height[l1]);
ans = (tmp > ans) ? tmp : ans;
}
if(height[l1] < height[l2])
{
l1++;
}
else if(height[l1] > height[l2])
{
l2--;
}
else
{
l1++; l2--;
}
}
return ans;
}

大神的代码就简洁很多:

int maxArea(vector<int> &height) {
int left = , right = height.size() - ;
int maxWater = ;
while(left < right){
maxWater = max(maxWater, (right - left) * min(height[left], height[right]));
height[left] < height[right] ? left++ : right--;
}
return maxWater;
}

【leetcode】Container With Most Water(middle)的更多相关文章

  1. 【leetcode】Search for a Range(middle)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  2. 【leetcode】Swap Nodes in Pairs (middle)

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  3. 【leetcode】Linked List Cycle II (middle)

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  4. 【leetcode】Reverse Linked List II (middle)

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  5. 【leetcode】Path Sum I & II(middle)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  6. 【leetcode】Binary Search Tree Iterator(middle)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  7. 【leetcode】Validate Binary Search Tree(middle)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  8. 【leetcode】Minimum Size Subarray Sum(middle)

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  9. 【leetcode】Evaluate Reverse Polish Notation(middle)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

随机推荐

  1. C#WebBrowser控件使用教程与技巧

    获取非input控件的值 webBrowser1.Document.All["控件ID"].InnerText;或webBrowser1.Document.GetElementBy ...

  2. Entity Framework 6.1-Code First

    原文:Entity Framework 6.1-Code First Code First-代码优先,先创建好领域模型.新建MyDbContext继承DbContext.根据代码自动生成数据库 Cod ...

  3. [转] Portable Trac 简单介绍 - 兼谈为什么不选择 Redmine

    Portable Trac 简单介绍 - 兼谈为什么不选择 Redmine ​Trac是一个轻量级的软件项目管理环境,如果在工作中涉及一个开发团队的管理并且关心项目管理工具的话,相信都在 ​Trac. ...

  4. Xcode-GitHub第三方库管理工具--CocoaPods

    一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库(从一个坑出来,又 ...

  5. Cocos2d-x手机游戏开发中-组合动作

    动作往往不是单一,而是复杂的组合.我们可以按照一定的次序将上述基本动作组合起来,形成连贯的一套组合动作.组合动作包括以下几类:顺序.并列.有限次数重复.无限次数重复.反动作和动画.动画我们会在下一节介 ...

  6. C# 高精度加法 支持小数(待优化)

    直接上代码 实现思路: 1.首先小数点补 位,9223372036854775808.9+9223372036854775808.9223372036854775808 => 922337203 ...

  7. css设置网页打印样式

    有三种方法 1. 为屏幕显示和打印分别准备一个css文件,如下所示:  用于屏幕显示的css: <link rel="stylesheet" href="css/n ...

  8. JavaScript、jQuery、HTML5、Node.js实例大全-读书笔记3

    技术很多,例子很多,只好慢慢学,慢慢实践!!现在学的这本书是[JavaScript实战----JavaScript.jQuery.HTML5.Node.js实例大全] JavaScript.jQuer ...

  9. 【leetcode】8. String to Integer (atoi)

    题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  10. Excel常用函数

    1.基本的算数函数 sum() average() 2.三角函数 sin() cos() 3.