题目:

  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.

翻译:

  给定n个非负整数a1,a2,...,an,其中每个代表一个点坐标(i,ai)。 n个垂直线段例如线段的两个端点在(i,ai)和(i,0)。 找到两个线段,与x轴形成一个容器,使其包含最多的水。

  备注:不考虑倾斜容器。

思路:容积其实就是面积,我们都知道长方形的面积=长*宽,不妨我们就从长最长的长方形找起,即令left = 0, right = height.size() - 1,但是在找下一个长方形时,长肯定会变短,要弥补这一段损失就必须加宽宽度,所以一下个就换掉两条宽中较小的那一个。

public class Solution {
public int maxArea(int[] height) {
int left=0;
int right=height.length-1;
int maxArea=0;
while(left<right){
maxArea=Math.max(maxArea,Math.min(height[left],height[right])*(right-left));
if(height[left]>height[right]){
right--;
}else{
left++;
}
}
return maxArea;
}
}

  

最新文章

  1. PostgreSQL隐藏字段tableoid
  2. C++的ORM工具比较
  3. Git Server &amp; Git Hook
  4. 【MySQL】binlog_format以及binlog事务记录分析
  5. **bootstrap常见常用样式总结
  6. 结构型模式——Adapter
  7. loadrunner_analysis技巧_filter和group by
  8. IIS6,IIS7中查看w3wp进程
  9. 【jQuery、原生】键盘键入两位小数
  10. JAVA基础--继承和权限控制
  11. Java中for_each循环的使用
  12. 【深度学习系列】用PaddlePaddle进行车牌识别(二)
  13. vue源码阅读(一)
  14. web@css高级选择器(after,befor用法),基本css样式
  15. [PHP] 算法-二叉树的子结构判断的PHP实现
  16. RabbitMQ 发布订阅-实现延时重试队列(参考)
  17. 两个简单的动态规划问题,0-1背包和最大不相邻数累加和,附递归c代码
  18. ubuntu 发送邮件
  19. Find non-overlap jobs with max cost
  20. aarch64_m2

热门文章

  1. 剑指offer系列28--字符流中第一个不重复的字符
  2. CSS媒体查询(@media)
  3. html body标签的几个属性 禁用鼠标右键,禁用鼠标选中文字等
  4. Exception error message with incorrect line number
  5. VS 开发工具中的Remote Debug 功能远程调试程序经验分享
  6. ADF_Starting系列4_使用ADF开发富Web应用程序之维护User Interface(Part1)
  7. PLSQL_性能优化系列19_Oracle Explain Plan解析计划通过Profile绑定
  8. CF 161D Distance in Tree 树形DP
  9. vs2012出现无法启动iis express web 服务器的错误
  10. ylbtech-dbs:ylbtech-6,record(生活记录)