Container With Most Water

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.

思路: 贪婪。从两端开始,计算面积。每次走一步,若左端高度较低,从左端走;反之,从右端走。

class Solution {
public:
int maxArea(vector<int> &height) {
int maxS = 0;
int it1 = 0, it2 = height.size()-1;
while(it1 < it2){
int S = min(height[it1], height[it2]) * (it2 - it1);
if(S > maxS) maxS = S;
if(height[it1] >= height[it2]) --it2;
else ++it1;
}
return maxS;
}
};

67. Container With Most Water的更多相关文章

  1. 刷题11. Container With Most Water

    一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...

  2. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  3. [LintCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  4. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  5. No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

  6. leetcode面试准备:Container With Most Water

    leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...

  7. 关于Container With Most Water的求解

    Container With Most Water 哎,最近心情烦躁,想在leetcode找找感觉,就看到了这题. 然而,看了题目半天,硬是没看懂,于是乎就百度了下,怕看到解题方法,就略看了下摘要,以 ...

  8. [leecode]---11.container with most water

    description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数 ...

  9. Leetcode11 Container With Most Water 解题思路 (Python)

    今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...

随机推荐

  1. HTML5全局属性和事件

    全局属性和事件能够应用到所有标签元素上,在HTML4中有许多全局属性,比如id,class等.HTML5中又新增了一些特殊功能的全局属性和事件.   属性:   HTML5属性能够赋给标签元素含义和语 ...

  2. easyui datagrid

    function initData(){ //数据列表 yftjsy=$("#yftjsy").datagrid({ url: '', fit:true, showFooter:f ...

  3. SQLAlchemy一对多总结

    1.SQLAlchemy之一对多关系 1.1 创建单表 class Test(Base): __tablename__ = 'user' nid = Colume(Integer,primary_ke ...

  4. HDU1215(筛选法)

    题意:求n的所有因子和: 思路:类似于筛选法求素数的思想,只有第一次的时候了解过它的思想,然后就只是用来求素数,思想的运用反而少: 筛选法求素数: int prime() { memset(vis, ...

  5. system_call的处理过程

    一. 跟踪time系统调用 使用gdb调试跟踪系统调用内核函数sys_time 过程如下: 对sys_time设置断点之后,在menuOS中执行time命令,发现系统停在systime处,输入S单步执 ...

  6. 开发经验之状态机思想,分别使用了swift,OC,C,PHP语言实现

    这里设计一个简单的练习,使用状态机思想实现,分别使用了swift,OC,C,PHP语言实现 题目:1到10000遍历,开始-打印奇数-遇到7的倍数开始打印偶数--遇到10的倍数打印奇数   //部分结 ...

  7. 第三个Sprint冲刺第六天

    讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论问题:研究一下有趣的东西

  8. json_decode时含有中文是解码问题(json_decode返回为null)

    function myDecode($str){ $str = substr(str_replace('\"','"',json_encode($str)),1,-1); retu ...

  9. charles 结合mocky 模拟数据

    重定向(模拟造数据) 例如:E代送商户端订单列表,模拟99+订单 接口:http://api.edaisong.com/20151022/order/consigneeaddressb 打开http: ...

  10. CDH自己安装方式Hbase master备份方式

    hbase-daemon.sh start master 启动改命令会默认产生Hmaster 进程,等待主机宕机,zookeepr 监控