LeetCode Array Medium 11. Container With Most Water
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.
The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example:
Input: [,,,,,,,,]
Output:
题目描述:给定n(n>=2)个非负整数,构成下面的图,找到两条线,它们与x轴一起形成一个容器,这样容器就含有最多的水。
思路:可以理解为求矩形的最大面积。将y轴的点到x轴的垂直距离作为矩形的一边,两个点的x轴的值的差作为另一边。求这两个边所形成的矩形的最大面积。
两个指针分别指向给定数组的头和尾,计算面积S=(两个点的y轴的最小值)* 两个点的x轴的差值。y轴的小的点在计算之后继续向中间靠拢。直到头尾指针相邻。
代码:
//11. Container With Most Water
public int MaxArea(int[] height)
{
int maxArea = int.MinValue;
int lo = , hi = height.Length - ;
while(lo < hi)
{
int temp = ;
if(height[lo] <= height[hi])
{
temp = height[lo] * (hi - lo);
lo++;
}
else
{
temp = height[hi] * (hi - lo);
hi--;
}
if (temp > maxArea)
maxArea = temp;
}
return maxArea;
}
LeetCode Array Medium 11. Container With Most Water的更多相关文章
- 【LeetCode two_pointer】11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- leetcode个人题解——#11 Container with most water
class Solution { public: int maxArea(vector<int>& height) { ; ; ; while(l < r) { int h ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 刷题11. Container With Most Water
一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [leecode]---11.container with most water
description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数 ...
- 蜗牛慢慢爬 LeetCode 11. Container With Most Water [Difficulty: Medium]
题目 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
随机推荐
- Unity打包IOS踩坑记
1. Xcode不显示模拟器 之前一直用真机调试,就没注意模拟器.今天要上传版本要用到模拟器截图,发现竟然Xcode的运行选项竟然没有显示模拟器. 也是网上找了各种方法,修改各种设置,清了各种文件夹都 ...
- 关于数位dp的一些思考
大致看完了claris的数位dp的pdf,感觉题目很厚实啊QAQ. 然后回过头再总结一下(感觉也不算总结啊,就是日常吐槽....) 首先数位dp这个东西是有格式的....所以明天早上再找道题来把模板联 ...
- linux基本命令的简单介绍
基本命令 man:查看帮助信息 :一般系统命令太多,要记住这些命令是不可能的,man是一个联机帮助信息 man提供大量的帮助信息,一般分为以下4各部分 NAME:对命令的简单介绍 SYNOPSIS对命 ...
- OpenCV/Python/dlib眨眼检测
今天我们来使用面部标志和OpenCV 检测和计算视频流中的眨眼次数. 为了构建我们的眨眼检测器,我们将计算一个称为眼睛纵横比(EAR)的指标,由Soukupová和Čech在其2016年的论文&quo ...
- STM8硬件设计注意事项
1.中断 STM8的外部中断和STM32不一样,每个端口PX只有1个中断 2.ADC 1)Additional AIN12 analog input is not selectable in ADC ...
- go语言从例子开始之Example8.数组
在 Go 中,数组 是一个固定长度的数列. package main import "fmt" func main() { 这里我们创建了一个数组 a 来存放刚好 5 个 int. ...
- linux-ftp install
ftp安装 yum install vsftpd vi /etc/vsftpd/vsftpd.conf (可以先备份,但不要放在此目录下,不然启动vsftpd的时候 也会加载其备份文件,报启动错误,因 ...
- Jenkins的安装、部署、启动(完整教程)
测试环境 Linux系统 Centos 7 安装步骤: 1.安装jdk 我安装的是jdk8,此处就不多说了,自己百度哈,很简单 2.安装jenkins 首先依次执行如下三个命令: 2.1.导入镜像: ...
- JavaSE---多线程---线程的创建、启动
1.概述 1.1 Java中使用Thread类表示线程: 所有的线程对象必须是Thread类 或 其子类的实例: 每条线程的作用:完成一定的任务: Java中使用run方法来封装线程执行体 ...
- php wordwrap()函数 语法
php wordwrap()函数 语法 wordwrap()函数怎么用? wordwrap()函数表示按照指定长度对字符串进行折行处理,语法是wordwrap(string,width,break,c ...