Chinaunix首页 | 论坛 | 博客
  • 博客访问: 42911
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 224
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-06 15:31
文章分类

全部博文(29)

文章存档

2015年(18)

2014年(11)

我的朋友

分类: Java

2015-01-22 17:33:11

题目:

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.
思路:最直观的解法:

点击(此处)折叠或打开

  1. public class Solution {
  2.     public int maxArea(int[] height){
  3.         int len = height.length;
  4.         int result = 0;
  5.         for(int i = 0; i < len; i++){
  6.             for(int j = i+1; j < len; j++){
  7.                 int tempresult = 0;
  8.                 if(height[i] < height[j]){
  9.                     tempresult = height[i] * (j-i);
  10.                 }
  11.                 else{
  12.                     tempresult = height[j] * (j-i);
  13.                 }
  14.                 if(tempresult > result){
  15.                     result = tempresult;
  16.                 }
  17.             }
  18.         }
  19.         return result;
  20.     }
  21. }
此算法的时间复杂度为O(n2),因此不会被OJ判定为通过。我们可以思考,对于该数组中的一个数m,以他为一条边的桶容积最大时应该满足什么条件,容易想到,只需要找到距离该数最远的一个大于该数的数组中的数n,n和m所组成的桶即为容积最大的桶。根据这种思路,可以从数组的第一个元素开始,从数组末尾开始寻找第一个大于该元素的元素。但是,有可能大于该元素的数全部位于该元素之前,例如当数组为倒序排列时。考虑倒序数组这一情况,此时我们的思路仍然是正确的,只是此时我们的遍历顺序应该从数组的末尾开始。应此,一个通用的解法可能是,从数组的开始与结尾同时开始遍历,依次找到每一个元素所能组成的最大容积的桶。那么可以写出程序:

点击(此处)折叠或打开

  1. public class Solution {
  2.     public int maxArea(int[] height) {
  3.         int len = height.length;
  4.         int result = 0;
  5.         int i = 0;
  6.         int j = len - 1;
  7.         while(i < len && j > i){
  8.          result = Math.max(result,(j-i)*Math.min(height[i],height[j]));
  9.          if(height[i] > height[j]){
  10.               j--;
  11.          }
  12.          else{
  13.               i++;
  14.          }
  15.         }
  16.         return result;
  17.     }
  18. }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
                      

阅读(445) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~