题目:

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.

代码:

class Solution {
public:
int maxArea(vector<int>& height) {
if ( height.size()< ) return ;
int max_area = ;
int left = ;
int right = height.size()-;
while ( left<right )
{
if ( height[left]<=height[right] )
{
max_area = std::max(max_area, (right-left)*height[left]);
left++;
}
else
{
max_area = std::max(max_area, (right-left)*height[right]);
right--;
}
}
return max_area;
}
};

tips:

试图用DP去做,但是没想出来;最后无奈落入了Greedy的俗套solution。

这个greedy的思路也是蛮巧的:从两头开始往中间greedy,头尾两个greedy一起变化才得到greedy的条件。

=======================================

第二次过这道题,这道题题意不清晰:如果选了某两个板子,就当其他板子不存在。

class Solution {
public:
int maxArea(vector<int>& height) {
int l = ;
int r = height.size()-;
int ret = ;
while ( l<r )
{
if ( height[l]<=height[r] )
{
ret = max(ret, (r-l)*height[l]);
l++;
}
else
{
ret = max(ret, (r-l)*height[r]);
r--;
}
}
return ret;
}
};

【Container With Most Water】cpp的更多相关文章

  1. leetcode 【 Container With Most Water 】python 实现

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

  2. 【Trapping Rain Water】cpp

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  3. Hdu 4738【求无向图的桥】.cpp

    题目: 曹操在长江上建立了一些点,点之间有一些边连着.如果这些点构成的无向图变成了连通图,那么曹操就无敌了.刘备为了防止曹操变得无敌,就打算去摧毁连接曹操的点的桥.但是诸葛亮把所有炸弹都带走了,只留下 ...

  4. 【Search a 2D Matrix】cpp

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  5. 【Search for a Range】cpp

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

  6. 【Merge K Sorted Lists】cpp

    题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  7. 【Merge Two Sorted Lists】cpp

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  8. 【Largest Rectangle in Histogram】cpp

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  9. 【Validate Binary Search Tree】cpp

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

随机推荐

  1. HDevEngine in .NET Applications MultiThreading

    Basics To use HDevEngine in Visual Studio .NET, you must add a reference to the HALCON/.NET assembly ...

  2. Windows服务器高并发处理IOCP(完成端口)详细说明

    一. 完成端口的优点 1. 我想只要是写过或者想要写C/S模式网络服务器端的朋友,都应该或多或少的听过完成端口的大名吧,完成端口会充分利用Windows内核来进行I/O的调度,是用于C/S通信模式中性 ...

  3. ASP.NET中登陆验证码的生成和输入验证码的验证

    一:验证码的生成实现代码 protected void Page_Load(object sender, EventArgs e)    {        string validateCode = ...

  4. shell 快速浏览

    总结自: https://github.com/qinjx/30min_guides/blob/master/shell.md: http://blog.itpub.net/14293828/view ...

  5. 汶川大地震中的SAP成都研究院

    5·12汶川地震,发生于北京时间(UTC+8)2008年5月12日(星期一)14时28分04秒,此次地震的面波震级 里氏震级达8.0Ms.矩震级达8.3Mw,地震烈度达到11度.地震波及大半个中国及亚 ...

  6. 轻松搞定Struts 2:三步走上手小入门

    零.Struts 2是啥? SSH.SSM.SSI如雷贯耳,Struts 2 —— 是的,就这样了... 一.Hello Struts2 1.核心包 2.struts.xml核心控制器配置 <f ...

  7. IOS autosizing(设置控件的固定位置大小)

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  8. Http协议--请求报文和响应报文

           http协议是位于应用层的协议,我们在日常浏览网页比如在导航网站请求百度首页的时候,会先通过http协议把请求做一个类似于编码的工作,发送给百度的服务器,然后在百度服务器响应请求时把相应 ...

  9. Gym - 100676H Capital City(边强连通分量 + 树的直径)

    H. Capital City[ Color: Black ]Bahosain has become the president of Byteland, he is doing his best t ...

  10. 【转】android调试工具DDMS的使用详解

    具体可见http://developer.android.com/tools/debugging/ddms.html. DDMS为IDE和emultor.真正的android设备架起来了一座桥梁.开发 ...