题目:

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;
}
};

最新文章

  1. SQLite3
  2. 【JavaScript】浅析javaScript和HTML与unicode字符集的关系
  3. 【翻译】ASP.NET MVC 5属性路由(转)
  4. spring security使用数据库资源
  5. HTTPS Everywhere – 保障隐私和信息安全的利器
  6. 利用powerdesigner反向数据库结构,生成ER图
  7. 【OpenMesh】Some basic operations: Flipping and collapsing edges
  8. c++ 使用全局变量的方法多个文件
  9. Spring Security 3.2.x与Spring 4.0.x的Maven依赖管理
  10. (GO_GTD_1)基于OpenCV和QT,建立Android图像处理程序
  11. 【Html5】-- 塔台管制
  12. zookeeper安装以及遇到的一些坑
  13. 12.JavaScript字符串方法
  14. PA教材提纲 TAW12-2
  15. 移植 Qt 至 tiny210 详细过程
  16. 黄聪:php7配置php.ini使其支持&lt;? ?&gt;
  17. Ajax技术剖析
  18. Linux系统软件包的管理(4)
  19. MySql 按周/月/日统计数据的方法
  20. windows7无声音,提示未插入扬声器或耳机的解决

热门文章

  1. tcpick
  2. 【微软大法好】VS Tools for AI全攻略(3):低配置虚拟机也能玩转深度学习,无需NC/NV系列
  3. java Vamei快速教程19 嵌套类
  4. Javascript 向量
  5. IOS PickerView使用
  6. iOS 制作表格 (数据源控制行,列数)
  7. 【转】chrome浏览器的跨域设置——包括版本49前后两种设置
  8. Linux 的歷史
  9. 题解 CF734A 【Anton and Danik】
  10. js call 函数