• [Leetcode 43] 11 Container With Most Water


    Problem:

    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.

    Analysis:

    Naive O(n^2) solution is too expensive.

    After searching on the web, find a O(n) solution. Similar to 2-sum, use two pointers points to the start and end of the given array, compute the area they can form and compare this result with the current max area. Update the max area if it's less than the new result. Then always choose the pointer which points to the shorter one, move it toward the center of the array and repeat step 1. 

    This solution is true because only two edges that both larger or equal to the current two edges can generate area that migth greater than the current area, which has a larger bottom. 

    Code:

    Naive solution

     1 class Solution {
     2 public:
     3     int maxArea(vector<int> &height) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int marea = 0;
     7         
     8         for (int i=0; i<height.size(); i++) {
     9             for (int j=i+1; j<height.size(); j++) {
    10                 int h = height[i] < height[j] ? height[i] : height[j];
    11                 int area = (j-i) * h;
    12                 if (area > marea) 
    13                     marea = area;
    14             }
    15         }
    16         
    17         return marea;
    18     }
    19 };
    View Code

    O(N) solution

     1 class Solution {
     2 public:
     3     int maxArea(vector<int> &height) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         int maxArea = 0, i = 0, j = height.size()-1;
     7         
     8         while (i < j) {
     9             int area = min(height[i], height[j]) * (j-i);
    10             if (area > maxArea)
    11                 maxArea = area;
    12                 
    13             if (height[i] < height[j])
    14                 i++;
    15             else
    16                 j--;
    17         }
    18         
    19         return maxArea;
    20     }
    21     
    22     int min(int a, int b) {
    23         return a<b?a:b;
    24     }
    25 };
    View Code

    Attention:

    O(n^2) solution can not be adopted if the data is too large.

  • 相关阅读:
    eclipse GWT开发环境的离线布置方法
    Eclipse(Myeclipse)安装GoogleGWT
    ASP.NET中的文件上传大小限制的问题
    ActivityCapture
    android camera 摄像头预览画面变形
    MUST_COMPLETE
    Stay true to yourself
    Android4.4 ContentResolver查询图片无效 及 图库删除 增加图片后,ContentResolver不更新的问题解决
    android 7.0以上共享文件(解决调用系统照相和图片剪切出现的FileUriExposedException崩溃问题)
    扫描指定目录下所有图片文件
  • 原文地址:https://www.cnblogs.com/freeneng/p/3096688.html
Copyright © 2020-2023  润新知