【数组】Container With Most Water
来源:互联网 发布:科学管理案例 知乎 编辑:程序博客网 时间:2024/05/01 17:59
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.
题意:从数组中找出两个值作为蓄水池的两个边,当然高度由矮的边决定解法:使用两个指针,分别指向数组的两端,移动短边的指针
public class Solution { public int area(int []a, int i, int j){ return Math.min(a[i], a[j]) * (j - i); } public int maxArea(int[] height) { int len = height.length; if(len <= 1) return 0; int l = 0, r = len-1; int max = 0; while(l < r){ max = Math.max(max, area(height, l ,r)); if(height[l] < height[r]) l++; else r--; } return max; }}
0 0
- 【数组】Container With Most Water
- 数组 Container With Most Water
- Container With Most Water
- Container with most water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Android API Guides 之 App Manifest(18) - <provider>
- FrameLayout布局
- [leetcode]Longest Valid Parentheses
- ssh中“Host key verification failed.“的解决方案
- .NET机房收费系统用例图分析
- 【数组】Container With Most Water
- Android API Guides 之 App Manifest(19) - <receiver>
- TCP 半关闭
- apk安装和运行
- Composite(组合模式)
- COM专题之一----开篇COM(组件对象模型)
- J2EE表单验证的前后台统一实现
- Java笔试面试(Web相关1)
- Android API Guides 之 App Manifest(20) - <service>