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. asterisk 语音文件转换

    Centos wav to sln sox foo-in.wav -t raw -r 8000 -s -2 -c 1 foo-out.sln 当前目录下所有语音wav文件 转换成sln for a i ...

  2. 【转】C#实现二叉查找树

    原文URL: http://www.cnblogs.com/CareySon/archive/2012/04/19/ImpleBinaryTreeWithCSharp.html   简介 树是一种非线 ...

  3. dom0级事件和dom2级事件

    dom0级事件 <a href="#" id="hash" onclick="fn();fn1();"> <button ...

  4. jqGrid API 相关

    取消所有选中的行: $("jqgridtableid").trigger("reloadGrid"): 设定选中行,可设定多行选中: $("jqgri ...

  5. 【leetcode】10.Regular Expression Matching

    题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

  6. 查看mysql集群状态是否正常

    如何查看mysql集群状态是否正常: 进入mysql 输入show status like 'wsrep%': 查看cluster sizes 是否为3

  7. 多文件上传 iOS功能

    多文件上传 iOS功能,原文来自ios教程网整理的,大家可以看看演示:ios.662p.com ,喜欢的朋友可以看看我的博客吧. NSURL* url = [NSURL URLWithString:@ ...

  8. java 枚举 类 enum

    public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializab ...

  9. Spring PecClinic宠物医院---安装

    1.下载源代码 如果本地安装了Git工具,可以直接使用命令 git clone https://github.com/spring-projects/spring-petclinic.git 如果没有 ...

  10. SSO单点登陆

    一句话,就是能让各个不同的域名带回相同的认证信息即可.实现方法,就是把其中一个登陆后,把认证的信息分别保存在不同域名下的 cookie,当在验证是否登陆时,验证 cookie,如果是子域名,这个则直接 ...