博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 11 Container with Most Water
阅读量:5149 次
发布时间:2019-06-13

本文共 3947 字,大约阅读时间需要 13 分钟。

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
using namespace std;int maxArea(vector
& height){ int max = 0; if (height.size() == 0) return 0; int length = height.size(); int area = 0; for(int i=0;i
height[i]?height[i]:height[j]; area = high*(j - i); if (max
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
& 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
using namespace std;///超时了int maxArea(vector
& height){ int max = 0; if (height.size() == 0) return 0; int length = height.size(); int area = 0; for(int i=0;i
height[i]?height[i]:height[j]; area = high*(j - i); if (max
& 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
array(0); array.push_back(1); array.push_back(1); int area = maxArea2(array); return 0;}

转载于:https://www.cnblogs.com/wangyaning/p/7853872.html

你可能感兴趣的文章
Ubuntu配置ssh及vnc
查看>>
C语言进阶——const 和 volatile 分析09
查看>>
字符串的查找删除
查看>>
NOI2018垫底记
查看>>
快速切题 poj 1002 487-3279 按规则处理 模拟 难度:0
查看>>
Codeforces Round #277 (Div. 2)
查看>>
一步步学Mybatis-搭建最简单的开发环境-开篇(1)
查看>>
微信小程序图片上传
查看>>
【更新】智能手机批量添加联系人
查看>>
NYOJ-128前缀式计算
查看>>
centos6.7 配置外网端口映射
查看>>
淡定,啊。数据唯一性
查看>>
深入理解 JavaScript 事件循环(一)— event loop
查看>>
Hive(7)-基本查询语句
查看>>
Redis快速入门
查看>>
注意java的对象引用
查看>>
C++ 面向对象 类成员函数this指针
查看>>
inline函数的总结
查看>>
【Jquery】$.Deferred 对象
查看>>
Python字符编码
查看>>