LeetCode11-Container With Most Water
来源:互联网 发布:手机淘宝店铺链接在哪 编辑:程序博客网 时间:2024/05/12 16:45
【题目】
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.
【思路】
两条垂线与x轴所围成的区域容水的多少与两条垂线中的短边和两个垂线间的距离有关;
分别从i=0 和j = array.length-1处同时向中间检索,计算当前状态并与最佳状态进行比较,优于则更新;
从两条垂线中选择较短的一边向内推动,成为下一个状态,直至两条垂线重合;
【Java代码】
public class Solution_11_container_with_most_water {public int maxArea(int[] height){int result = 0;int i = 0, j = height.length - 1;while(j > i){int area = (height[j] > height[i] ? height[i]:height[j])*(j-i);result = area>result?area:result;if(height[j]>height[i])i++;else j--;}return result;}}
0 0
- Leetcode11: Container with most water
- [LeetCode11]Container With Most Water
- LeetCode11:Container With Most Water
- leetcode11 Container With Most Water
- leetcode11 Container With Most Water
- LeetCode11:Container With Most Water
- Leetcode11 Container With Most Water
- leetcode11:Container With Most Water
- LeetCode11: Container With Most Water
- leetcode11. Container With Most Water
- leetcode11. Container With Most Water
- LeetCode11-Container With Most Water
- LeetCode11. Container With Most Water
- LeetCode11. Container With Most Water
- LeetCode11. Container With Most Water
- leetcode11. Container With Most Water
- LeetCode11. Container With Most Water
- LeetCode11——Container With Most Water
- mysql 存储过程+mybatis
- [Python学习] python 科学计算库NumPy—tile函数用法
- JS实现选择器全选、反选
- JConsole使用
- MyEclipse2014修改Web-root folder的值
- LeetCode11-Container With Most Water
- ubuntu16.04 查看内核,升级内核,删除内核,切换内核
- JDBC驱动
- 机器学习----降维与度量学习(PCA)
- 地图坐标转换
- NYOJ228 士兵杀敌(五)(思路)
- BAT(批处理脚本)实现文件的安装和卸载(二)
- react native webView 加载没反应和基本使用
- javaOOP和数据库连接