zoukankan      html  css  js  c++  java
  • [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.

  • 相关阅读:
    Codeforces Round #251 (Div. 2) A
    topcoder SRM 623 DIV2 CatAndRat
    topcoder SRM 623 DIV2 CatchTheBeatEasy
    topcoder SRM 622 DIV2 FibonacciDiv2
    topcoder SRM 622 DIV2 BoxesDiv2
    Leetcode Linked List Cycle II
    leetcode Linked List Cycle
    Leetcode Search Insert Position
    关于vim插件
    Codeforces Round #248 (Div. 2) B. Kuriyama Mirai's Stones
  • 原文地址:https://www.cnblogs.com/freeneng/p/3096688.html
Copyright © 2011-2022 走看看