leetcode 11. Container With Most Water
2016-04-29 14:25
411 查看
//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 static void main(String[] args) { int[] a = {1,2,3,4}; int result = maxArea(a); System.out.println(result); } public static int maxArea(int[] height) { // int result = 0; //遍历height数组时间复杂度太大,无法通过 // int maxArea = 0; // for(int i = 0;i<height.length;i++){ // for(int j = i+1;j<height.length;j++){ // if(height[i]<height[j]){ // maxArea = (j-i)*height[i]; // }else{ // maxArea = (j-i)*height[j]; // } // // if(result<maxArea){ // result = maxArea; // } // } // } // // return result; int result = 0; int maxArea = 0; int left = 0; int right = height.length-1; while(right>left){ //从两头向中间遍历height if(height[left]>height[right]){ //若左边的高度大于右边,则将right左移 maxArea = height[right]*(right-left); right--; }else{ //若右边高度大于左边,则将left右移 maxArea = height[left]*(right-left); left++; } if(maxArea>result){ //找到最大的容积 result = maxArea; } } return result; } }
相关文章推荐
- stitching detail输出的dot图含义
- 从零开始写聊天机器人
- AI教程!教你绘制扁平化风格的卡通人物肖像(四人组合篇)
- UVa 10976 Fractions Again?!
- RAID详解[RAID0/RAID1/RAID10/RAID5]
- Exception raised during rendering: Binary XML file line #-1: No start tag found!
- svmtrain和svmpredict参数
- 当人工智能遇见金融财经
- 线程中sleep和wait的区别
- memcache的使用http://blog.csdn.net/scelong/article/details/7245343
- 217. Contains Duplicate
- Metastore contains multiple versions
- http://blog.csdn.net/hahalzb/article/details/5889545
- 人工智能正在向经济学领域渗透
- TensorFlow人工智能入门教程之十一 最强网络DLSTM 双向长短期记忆网络(阿里小AI实现)
- Intellij IdeaI快捷键
- Codeforces #325 Div2 D Phillip and Trains(模拟)
- CLOSE_WAIT状态的讨论
- CLOSE_WAIT状态的原因与解决方法
- TensorFlow人工智能引擎入门教程之十 最强网络 RSNN深度残差网络 平均准确率96-99%