题目:

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. js addEventListener调用传参函数

    先看这段代码 function abc(key){ console.log(key); } for(let i=0;i<oInput.length;i++){ oInput[i].addEven ...

  2. Cypress测试工具

    参考博客:  https://testerhome.com/articles/19035 最近一段时间学习了cypress的测试工具, 她是一个端到端的测试web工具. 环境准备 1.工具:vs co ...

  3. 如何在Ubuntu server中修改IP

    详细请移步至博客https://blog.csdn.net/shenzhen_zsw/article/details/74025066 方法一. sudo  ifconfig eth0 100.100 ...

  4. 部署JavaWeb时出现 If a file is locked,you can wait until

    在部署JavaWeb程序时出现了if a file is locked ,you can wait until the lock stop的问题,这个主要是classpath目录出错或者jar包未导入 ...

  5. linux 命令——14 head (转)

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...

  6. [Rails学习之路]Rails路由配置

    如果是使用Rails的默认约定,那么几乎是零配置. 但有些时候,我们可能不得不(或者更喜欢)进行一些特殊的配置. 其实Rails在路由功能中也有很丰富的配置选项. routes.rb文件中靠前的规则优 ...

  7. 【BZOJ1257】[CQOI2007] 余数之和(数学题)

    点此看题面 大致题意: 求\(\sum_{i=1}^nk\%i\). 关于除法分块 这是一道除法分块的简单应用题. 式子转换 显然\(k\%i\)是一个很难处理的项. 于是我们就要使用使用一个常用的套 ...

  8. 【51nod1705】七星剑(成环DP)

    点此看题面 大致题意: 你要把一把剑从0星升至7星,有n颗宝石供你选择,第i颗宝石的价值是c[i],用第i颗宝石将剑从k-1星升至k星的成功率是prob[k][i],而失败后会掉lose[k][i], ...

  9. 安装Ubuntu桌面环境后只能Guest登录的解决办法

    1.安装Ubuntu桌面环境后,登录界面只显示了Guest 2.在登录界面按住crtl+shift+F1,进入tty模式 3.输入sudo -s进入root模式 4.输入vi /etc/lightdm ...

  10. STMS传输队列中的请求状态一直是Running不能结束

    通过STMS传输请求时,遇到了如下问题: STMS传输请求,不论等多久的时间,请求状态一直是running,不能结束.但检查传输的内容时,发现CHANGE REQUEST包含的内容已经传输到目标Cli ...