leetcode-algorithms-11 Container With Most Water
leetcode-algorithms-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: [1,8,6,2,5,4,8,3,7]
Output: 49
解法1
列出所有情况,找到最大值.
class Solution
{
public:
int maxArea(vector<int>& height)
{
int area = 0;
for (int i = 0; i < height.size(); ++i)
{
for (int j = i + 1; j < height.size(); ++j)
{
int low = height[i] > height[j] ? height[j] : height[i];
int temp_area = low * (j - i);
if (temp_area > area)
area = temp_area;
}
}
return area;
}
};
时间复杂度: O(n^2).
空间复杂度: O(1).
解法2
要算出最大的面积,不考虑高度的情况下,肯定是长度越长,面积越大.接着,长度减小,面积是由短的那个决定的,因此,要使得到面积更大,就只需要移动短的那个高度.所以可得到下面的算法.
class Solution {
public:
int maxArea(vector<int>& height) {
int area = 0;
int left = 0;
int right = height.size() - 1;
while(left < right)
{
int low = height[left] < height[right] ? height[left] : height[right];
int temp_area = low * (right - left);
if (temp_area > area)
area = temp_area;
if (height[left] < height[right])
++left;
else
--right;
}
return area;
}
};
时间复杂度: O(n).
空间复杂度: O(1).
leetcode-algorithms-11 Container With Most Water的更多相关文章
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】11. Container With Most Water 盛最多水的容器
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...
- 【LeetCode】11. Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- leetcode problem 11 Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode OJ 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Leetcode Array 11 Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- 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 ...
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
随机推荐
- 检测浏览器(BOM)以及地址栏网址的API
navigator.userAgent //检测浏览器的版本以及那个厂商的 (不怎么准,你比如360经常跟别人干架,所以别人检测到360浏览器就提示浏览器危险,所以360就自己修改了) //分解这个地 ...
- 函数嵌套函数传递this值
<button onclick="demo()(this)">test</button> function demo(){ return function ...
- Python多线程爬虫
前言 用上多线程,感觉爬虫跑起来带着风 运行情况 爬取了9万多条文本记录,耗时比较短,一会儿就是几千条 关键点 多个线程对同一全局变量进行修改要加锁 # 获取锁,用于线程同步 threadLock.a ...
- WaitingFormHelper
using Lba_Ciac; using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- 生存分析与R--转载
生存分析与R 生存分析是将事件的结果和出现这一结果所经历的时间结合起来分析的一类统计分析方法.不仅考虑事件是否出现,而且还考虑事件出现的时间长短,因此这类方法也被称为事件时间分析(time-to-ev ...
- 【Java】【反射】
一,java的核心机制 java有两种核心机制:java虚拟机(JavaVirtual Machine)与垃圾收集机制(Garbage collection): Java虚拟机:是运行所有Java程序 ...
- CSS3动画详解(结合实例)
一.使用CSS3动画代替JS动画 JS动画频繁操作DOM导致效率非常低 在频繁的操作DOM和CSS时,浏览器会不停的执行重排(reflow)和重绘(repaint) 可以避免占用JS主线程 这边就不细 ...
- mac 下安装ES 与 Head插件 以及安装Kibana
一.安装Elasticsearch 在Mac上可以使用brew快速安装Elasticsearch brew install elasticsearch 安装完成后可使用elasticsearch -- ...
- Ajax请求导出Excel的问题
文章转载自: http://yuwenlin.iteye.com/blog/2275289 Ajax请求导出Excel的问题描述: 前端发起Ajax请求get或post,后台使用Poi生成excel文 ...
- Nginx的安装和使用(Linux)
关于什么是Nginx,Nginx的优势和使用范围这里就不多说了.其实它就是一个web服务器.这篇文章主要是说Nginx的安装和使用. 安装方式有yum安装和源码安装,这里主要讲源码安装 1.安装依赖, ...