11. Container With Most Water

题面

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

给定n个非负数字,代表n条垂线,在垂线之间装水,找出能装水最多的情况。

样例(如上图)

Example:

Input: [1,,6,2,5,4,8,3,]
Output: 49
statement: (8-1)*min(8, 7)

思路1:

暴力法:遍历所有可能情况,找出最大值。

时间复杂度:O(n2)

空间复杂度:O(1)

 class Solution {
public:
int maxArea(vector<int>& height) {
int size = height.size();
if(size == )
return min(height[], height[]);
int res = ;
for(int i=; i<size; i++)
{
for(int j=i+; j<size; j++)
{
int tmp = (j-i)*min(height[j],height[i]);
if(tmp > res)
res = tmp;
}
}
return res;
}
};

思路2:

两点逼近思想

1. 先取最左端和和最右端,然后计算容积,更新容积;

2. 更新l and r, 那一端长度height[]短,那一段就更新(++/--);

3. 输出最大容积。

时间复杂度:O(n)

空间复杂度:O(1)

 class Solution {
public:
int maxArea(vector<int>& height) {
int size = height.size();
if(size == )
return min(height[], height[]);
int res = ;
int l =, r = size-;
while(l < r)
{
int tmp = (r-l)*min(height[r],height[l]);
if(tmp > res)
res = tmp;
if(height[l] > height[r])
r--;
else
l++;
}
return res;
}
};

Array+DP leetcode-11.装更多的水的更多相关文章

  1. LeetCode 11. Container With Most Water (装最多水的容器)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  2. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  3. 江苏 徐州邀请赛 icpc B Array dp 滚动数组模板

    题目 题目描述 JSZKC is the captain of the lala team. There are N girls in the lala team. And their height ...

  4. 2021.11.30 eleveni的水省选题的记录

    2021.11.30 eleveni的水省选题的记录 因为eleveni比较菜,eleveni决定先刷图论,再刷数据结构,同时每天都要刷dp.当然,对于擅长的图论,eleveni决定从蓝题开始刷.当然 ...

  5. 2021.11.02 eleveni的水省选题的记录

    2021.11.02 eleveni的水省选题的记录 因为eleveni比较菜,所以eleveni决定从绿题开始水 --实际上菜菜的eleveni连绿题都不一定能水过/忍不住哭了 [P2217 HAO ...

  6. [LeetCode] 11. Container With Most Water My Submissions Question 解题思路

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  7. Educational Codeforces Round 56 (Rated for Div. 2) F - Vasya and Array dp好题

    F - Vasya and Array dp[ i ][ j ] 表示用了前 i 个数字并且最后一个数字是 j 的方案数. dp[ i ][ j ] = sumdp [i - 1 ][ j ], 这样 ...

  8. 2021.11.05 eleveni的水省选题的记录

    2021.11.05 eleveni的水省选题的记录 因为eleveni比较菜,但是eleveni不想写绿题(总不能说是被绿题虐得不想写),eleveni决定继续水noip原题. --实际上菜菜的el ...

  9. [LeetCode] 11. Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

随机推荐

  1. 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例

    这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...

  2. LeetCode_172. Factorial Trailing Zeroes

    172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...

  3. .NET C#获取当前网页地址信息

    设当前页完整地址是:http://www.qiandabao.com/aaa/bbb.aspx?id=5&name=kelli "http://"是协议名"www ...

  4. VS2010插件之NuGet

    Visual Studio(简写VS)是.net程序员开发必不可少的开发工具,随着VS的版本不断的升级和使用用户的扩大,现在针对VS开发了许多的开源免费的插件,大大的方便了程序员的开发,提高了开发效率 ...

  5. react 组装table列表带分页

    2.组装编辑界面 /** * Created by hldev on 17-6-14. */ import React, {Component} from "react"; imp ...

  6. 【c# 学习笔记】c#中的语句

    1.条件语句: if语句: bool condition = true; if (condition) { } else if (condition) { } else { } switch语句: b ...

  7. 《CNCF × Alibaba云原生技术公开课》知识点自测(三):Kubernetes核心概念

    (单选)1.Kubernetes的中文含义是___. A. 船   B.舵手  C.容器平台  D.起重机 (单选) 2.Kubectl是_____. A. 一个与Kubernetes集群进行交互.管 ...

  8. ELK7.4.2安装教程

    ELK简介 "ELK"是三个开源项目的首字母缩写,这三个项目分别是:Elasticsearch.Logstash 和 Kibana.Elasticsearch 是一个搜索和分析引擎 ...

  9. 最新 二六三网络通信java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿. 二六三网络通信等10家互联网公司的校招Offer,因为某些自身原因最终选择了 二六三网络通信.6.7月主要是做系统复习.项 ...

  10. 日常工作问题解决:du命令详解

    目录 1.导读 1.1 命令格式 1.2 命令功能 1.3 命令参数 2.实例 2.1 实例1:显示目录或者文件所占空间 2.2 实例2:显示指定文件所占空间 2.3 实例3:查看指定目录所占空间 2 ...