首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode 11 Container with Most Water

leetcode 11 Container with Most Water

作者头像
流川疯
发布2019-01-18 15:52:36
4030
发布2019-01-18 15:52:36
举报

1.题目描述

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 and n is at least 2.

2.中文解释:

给定n个非负整数a1,a2,…,an,其中每个代表一个点坐标(i,ai)。

n个垂直线段例如线段的两个端点在(i,ai)和(i,0)。

找到两个线段,与x轴形成一个容器,使其包含最多的水。

备注:你不必倾倒容器。


3.超时的c++算法

当然,谁都可以想到的解法就是暴力匹配,当遇到等差数列的时候当然就超时了!!!

这里写图片描述
这里写图片描述
// leetcode11.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<vector>
using namespace std;


int maxArea(vector<int>& height)
{
    int max = 0;
    if (height.size() == 0) return 0;

    int length = height.size();
    int area = 0;
    for(int i=0;i<length;i++)
    {
        for (int j = i; j < length; j++)
        {
            int high = height[j] > height[i]?height[i]:height[j];
            area = high*(j - i);
            if (max<area)
            {
                max = area;

            }
        }

    }


    return max;
}
int main()
{
    vector<int> array(10);
    array.push_back(1);
    array.push_back(1);

    int area = maxArea(array);
    return 0;
}

4.正确答案

这里写图片描述
这里写图片描述

算法证明:

Here is the proof. Proved by contradiction:

Suppose the returned result is not the optimal solution. Then there must exist an optimal solution, say a container with a_ol and a_or (left and right respectively), such that it has a greater volume than the one we got. Since our algorithm stops only if the two pointers meet. So, we must have visited one of them but not the other. WLOG, let’s say we visited a_ol but not a_or. When a pointer stops at a_ol, it won’t move until

The other pointer also points to a_ol. In this case, iteration ends. But the other pointer must have visited a_or on its way from right end to a_ol. Contradiction to our assumption that we didn’t visit a_or.

The other pointer arrives at a value, say a_rr, that is greater than a_ol before it reaches a_or. In this case, we does move a_ol. But notice that the volume of a_ol and a_rr is already greater than a_ol and a_or (as it is wider and heigher), which means that a_ol and a_or is not the optimal solution – Contradiction!

Both cases arrive at a contradiction.

参考答案:

///C++

int maxArea(vector<int>& height) {
    int water = 0;
    int i = 0, j = height.size() - 1;
    while (i < j) {
        int h = min(height[i], height[j]);
        water = max(water, (j - i) * h);
        while (height[i] <= h && i < j) i++;
        while (height[j] <= h && i < j) j--;
    }
    return water;
}
///C语言参考答案

A bit shorter and perhaps faster because I can use raw int pointers, but a bit longer because I don't have min and max.

int maxArea(int* heights, int n) {
    int water = 0, *i = heights, *j = i + n - 1;
    while (i < j) {
        int h = *i < *j ? *i : *j;
        int w = (j - i) * h;
        if (w > water) water = w;
        while (*i <= h && i < j) i++;
        while (*j <= h && i < j) j--;
    }
    return water;
}

python参考答案

class Solution:
    def maxArea(self, height):
        i, j = 0, len(height) - 1
        water = 0
        while i < j:
            water = max(water, (j - i) * min(height[i], height[j]))
            if height[i] < height[j]:
                i += 1
            else:
                j -= 1
        return water

我的完整工程:

// leetcode11.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<vector>
using namespace std;

///超时了
int maxArea(vector<int>& height)
{
    int max = 0;
    if (height.size() == 0) return 0;

    int length = height.size();
    int area = 0;
    for(int i=0;i<length;i++)
    {
        for (int j = i; j < length; j++)
        {
            int high = height[j] > height[i]?height[i]:height[j];
            area = high*(j - i);
            if (max<area)
            {
                max = area;

            }
        }

    }


    return max;
}

///accept
int maxArea2(vector<int>& height)
{
    int max = 0;
    if (height.size() == 0) return 0;

    int length = height.size();
    int area = 0;
    int l = 0;
    int r = length - 1;

    while (l < r)
    {
        int high = height[l] > height[r] ? height[r] : height[l];
        max = max > (high * (r - l)) ?  max : (high * (r - l));
        if (height[l] < height[r])
            l++;
        else
            r--;
    }

    return max;
}
int main()
{
    vector<int> array(0);
    array.push_back(1);
    array.push_back(1);

    int area = maxArea2(array);
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年04月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.题目描述
  • 2.中文解释:
  • 3.超时的c++算法
  • 4.正确答案
    • 算法证明:
    相关产品与服务
    容器服务
    腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档