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. 设置win7任务栏显示标题,而不显示缩略图

    win7系统的任务栏可以显示桌面缩略图,这是非常好的一个功能,但是有时候我们希望只显示标题,如下所示 怎样设置呢?只要在桌面上的计算机图标上面“右键”,选择“属性”,在弹出的窗口选择“高级系统设置”, ...

  2. orcale 循环插入 测试数据

    以前开发一直用的是sql server   定义临时变量 循环插入数据到表中已经成为一种固定的模式,本来想orcale应该也一样吧 都是数据库.. 结果被现实无情的打击到了.在网上找办法,求大神 最后 ...

  3. 第十篇、Swift -- WebSocket

    每当小编再开发中遇到了困难,在网上搜,简直是垃圾堆里找金子.国内网站真的全不可靠,最后FQ去国外网站寻找,才可以找到.找到了写websocket文章,同时找到了集成的框架文件,一个叫Starscrea ...

  4. UIView中常见的方法总结

    addSubview: 添加一个子视图到接收者并让它在最上面显示出来.- (void)addSubview:(UIView *)view[讨论]这方法同样设置了接收者为下一个视图响应对象.接收者保留视 ...

  5. C# 线程--第四线程实例

    概述 在前面几节中和大家分享了线程的一些基础使用方法,本章结合之前的分享来编写一些日常开发中应用实例,和编写多线程时一些注意点.如大家有好的实例也欢迎分享.. 应用实例 应用:定时任务程序 场景:系统 ...

  6. CSS笔记---文字两边对齐

    <style> .box{ width: 1000px; height: 500px; background-color: #aa0000; margin:0 auto; } .teste ...

  7. 暑假集训(4)第七弹——— 组合(hdu1850)

    题意概括:你赢得了第一局.魔鬼给出的第二局是,如果有N堆牌,先手的人有几种可能胜利. 问题分析:尼姆游戏,先得到n堆牌的数量异或和,再将异或和与每一个牌组的数量异或,如果结果小于原牌组数量 则可能++ ...

  8. vm安装mac系统

    零:起因想学习IOS开发,但是mac pro和mac mini都舍不得买,去apple店体验了几次mac不怎么喜欢,so… 一:原料VMware-workstation-full-12.0.0-298 ...

  9. 专家解说IT行业存在哪些安全风险

    本人为原创作品:e良师益友 ,转载是并且注明 网络带动了IT行业的发展,同时也带来了安全风险,国外的专家分析2014年IT行业存在的11个安全隐患,随着越来越多技术的不断开发,为方便人们记忆账号,产生 ...

  10. mysql的时间函数

    from_unixtime()是MySQL里的时间函数 date为需要处理的参数(该参数是Unix 时间戳),可以是字段名,也可以直接是Unix 时间戳字符串 后面的 '%Y%m%d' 主要是将返回值 ...