LeetCode Solutions : Container With Most Water
来源:互联网 发布:linux dhcp分配主机名 编辑:程序博客网 时间:2024/06/06 17:05
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.
Note: You may not slant the container.
Java Solutions:
1.
public class Solution { public int maxArea(int[] height) { int areas=0;int max_areas;for(int i=0,j=height.length-1;j>i;){max_areas=Math.min(height[i],height[j])*(j-i);if(max_areas>areas)areas=max_areas;if(height[i]>height[j]){j--;}else{i++;}}return areas; }}
2. After Improvement :
public class Solution { public int maxArea(int[] height) { int areas=0;int max_areas;int hl=height[0];int hr=height[height.length-1];for(int i=0,j=height.length-1;j>i;){max_areas=Math.min(hl,hr)*(j-i);if(max_areas>areas)areas=max_areas;if(hl>hr){while(i<j&&height[j]<=hr)j--;if(j>i)hr=height[j];}else{while(i<j&&height[i]<=hl)i++;if(j>i)hl=height[i];}}return areas; }}
0 0
- LeetCode Solutions : Container With Most Water
- LeetCode: Container With Most Water
- [LeetCode]Container With Most Water
- LeetCode Container With Most Water
- LeetCode: Container With Most Water
- [Leetcode] Container With Most Water
- Leetcode: Container With Most Water
- [LeetCode] Container With Most Water
- LeetCode Container With Most Water
- [Leetcode] Container With Most Water
- [LeetCode]Container With Most Water
- LeetCode-Container With Most Water
- Container With Most Water leetcode
- LeetCode - Container With Most Water
- LeetCode: Container With Most Water
- Leetcode: Container With Most Water
- 【LeetCode】Container With Most Water
- Leetcode: Container With Most Water
- 【漫立方译】交互式GPU光线追踪——用于基于物理的灯光预览
- hdu 1003 求连续子数列的最大和
- 职业规划
- SSH中如何进行单元测试
- 【漫立方译】交互式GPU光线追踪——用于基于物理的灯光预览
- LeetCode Solutions : Container With Most Water
- hdu 1085
- SpringMVC学习笔记(5) 启用(优化)注解+静态资源访问
- 1001 Exponentiation
- hdu round7 Magical Forest(map)
- 项目Mooc修改任务
- 怎么获取domino里面所有注册的用户名
- 次小生成树
- Xml学习