LeetCode - 11. Container With Most Water

11. Container With Most Water #

Problem's Link
#

 ----------------------------------------------------------------------------

Mean: 

给你一个N条垂直于x轴的直线,从中找两条直线和x轴组成一个桶状容器,使得这个容器的容量最大.

analyse:

1.O(n^2)的做法就不说了,妥妥的超时.

for(int i=0;i<n;++i)
   for(int j=i+1;j<n;++j)
       ...

2.后来在上面方法的基础上加了一些剪枝,但复杂度还是O(n^2),也超时.

// Time Limit Exceeded
class Solution
{
public:
   int maxArea(vector<int>& height)
   {
       if(height.size()<=1)
           return 0;
       vector<int> frontIdx;
       frontIdx.push_back(0);
       auto ret=0;
       for(auto i=1;i<height.size();++i)
       {
           for(auto idx:frontIdx)
           {
               ret=max(ret,min(height[idx],height[i])*(i-idx));
           }
           auto endIdx=*frontIdx.rbegin();
           if(height[i]>height[endIdx])
               frontIdx.push_back(height[i]);
       }
       return ret;
   }
};

 3.后来看了discuss,看到这个做法,甚是巧妙.

proof:

The O(n) solution with proof by contradiction doesn't look intuitive enough to me. Before moving on, read the algorithm first if you don't know it yet.

Here's another way to see what happens in a matrix representation:

Draw a matrix where the row is the first line, and the column is the second line. For example, say n=6.

In the figures below, x means we don't need to compute the volume for that case: (1) On the diagonal, the two lines are overlapped; (2) The lower left triangle area of the matrix is symmetric to the upper right area.

We start by computing the volume at (1,6), denoted by o. Now if the left line is shorter than the right line, then all the elements left to (1,6) on the first row have smaller volume, so we don't need to compute those cases (crossed by ---).

复制代码
  1 2 3 4 5 6
1 x ------- o
2 x x
3 x x x 
4 x x x x
5 x x x x x
6 x x x x x x
复制代码

 Next we move the left line and compute (2,6). Now if the right line is shorter, all cases below(2,6) are eliminated.

复制代码
  1 2 3 4 5 6
1 x ------- o
2 x x       o
3 x x x     |
4 x x x x   |
5 x x x x x |
6 x x x x x x
复制代码

And no matter how this o path goes, we end up only need to find the max value on this path, which contains n-1 cases.

复制代码
  1 2 3 4 5 6
1 x ------- o
2 x x - o o o
3 x x x o | |
4 x x x x | |
5 x x x x x |
6 x x x x x x
复制代码
class Solution
{
public:
   int maxArea(vector<int>& height)
   {
       int si=height.size();
       int low=0,high=si-1;
       int ret=0;
       while(low<high)
       {
           ret=max(ret,min(height[low],height[high])*(high-low));
           if(height[low]<height[high])
               ++low;
           else
               --high;
       }
       return ret;
   }
};

Time complexity: O(N)

 

view code

/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
*       Author: crazyacking
*       Date  : 2016-02-16-09.42
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
/*
// Time Limit Exceeded
class Solution
{
public:
   int maxArea(vector<int>& height)
   {
       if(height.size()<=1)
           return 0;
       vector<int> frontIdx;
       frontIdx.push_back(0);
       auto ret=0;
       for(auto i=1;i<height.size();++i)
       {
           for(auto idx:frontIdx)
           {
               ret=max(ret,min(height[idx],height[i])*(i-idx));
           }
           auto endIdx=*frontIdx.rbegin();
           if(height[i]>height[endIdx])
               frontIdx.push_back(height[i]);
       }
       return ret;
   }
};
*/

//for(int i=0;i<n;++i)
//    for(int j=i+1;j<n;++j)
//        ...

class Solution
{
public:
   int maxArea(vector<int>& height)
   {
       int si=height.size();
       int low=0,high=si-1;
       int ret=0;
       while(low<high)
       {
           ret=max(ret,min(height[low],height[high])*(high-low));
           if(height[low]<height[high])
               ++low;
           else
               --high;
       }
       return ret;
   }
};

int main()
{
   Solution solution;
   auto n=0;
   while(cin>>n)
   {
       vector<int> ve;
       for(int i=0; i<n; ++i)
       {
           int tmp;
           cin>>tmp;
           ve.push_back(tmp);
       }
       cout<<solution.maxArea(ve)<<endl;
   }
   return 0;
}
posted @   北岛知寒  阅读(217)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2014-12-10 C++ - 多线程的实现
点击右上角即可分享
微信分享提示
主题色彩